{"id":2995,"date":"2021-11-19T09:00:00","date_gmt":"2021-11-19T08:00:00","guid":{"rendered":"http:\/\/www.albertopasca.it\/whiletrue\/?p=2995"},"modified":"2021-11-27T16:42:12","modified_gmt":"2021-11-27T15:42:12","slug":"swift-have-fun-with-gravitybehavior-and-dynamicanimator","status":"publish","type":"post","link":"http:\/\/www.albertopasca.it\/whiletrue\/swift-have-fun-with-gravitybehavior-and-dynamicanimator\/","title":{"rendered":"Swift &#8211; Have fun with GravityBehavior and DynamicAnimator"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Today a funny &#x1f60e; way to use <em>device motion<\/em> to move using the <em>gravity<\/em> the objects on your view controller, like this:<\/p>\n\n\n\n<!--more-->\n\n\n\n<figure class=\"wp-block-video aligncenter\"><video height=\"1920\" style=\"aspect-ratio: 886 \/ 1920;\" width=\"886\" autoplay loop muted src=\"http:\/\/www.albertopasca.it\/whiletrue\/wp-content\/uploads\/2021\/11\/RPReplay_Final1637240562.mp4\"><\/video><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-background has-vivid-cyan-blue-background-color has-vivid-cyan-blue-color\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Setup<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">For this demo the interface is very simple: add lots of default view objects, like buttons, labels, switches, etc&#8230; &#x1fa84;<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"749\" height=\"1024\" src=\"https:\/\/i0.wp.com\/www.albertopasca.it\/whiletrue\/wp-content\/uploads\/2021\/11\/Schermata-2021-11-18-alle-14.08.27.png?resize=749%2C1024\" alt=\"\" class=\"wp-image-2997\" srcset=\"https:\/\/i0.wp.com\/www.albertopasca.it\/whiletrue\/wp-content\/uploads\/2021\/11\/Schermata-2021-11-18-alle-14.08.27.png?resize=749%2C1024 749w, https:\/\/i0.wp.com\/www.albertopasca.it\/whiletrue\/wp-content\/uploads\/2021\/11\/Schermata-2021-11-18-alle-14.08.27.png?resize=220%2C300 220w, https:\/\/i0.wp.com\/www.albertopasca.it\/whiletrue\/wp-content\/uploads\/2021\/11\/Schermata-2021-11-18-alle-14.08.27.png?w=764 764w\" sizes=\"auto, (max-width: 749px) 100vw, 749px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">And connect everything via <strong>IBOutlets<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"swift\" class=\"language-swift\">    @IBOutlet weak var label1: UILabel!\n    @IBOutlet weak var label2: UILabel!\n    @IBOutlet weak var button1: UIButton!\n    @IBOutlet weak var button2: UIButton!\n    @IBOutlet weak var switch1: UISwitch!\n    @IBOutlet weak var switch2: UISwitch!<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-background has-vivid-red-background-color has-vivid-red-color\"\/>\n\n\n\n<p class=\"wp-block-paragraph\">Now we need to add our code. First of all import the <strong>CoreMotion<\/strong> sdk:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"swift\" class=\"language-swift\">import CoreMotion<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">next create the classes you need to create this effects: <strong>UIDynamicAnimator<\/strong>, <strong>UIGravityBehaviour<\/strong> and <strong>CMMotionManager<\/strong>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"swift\" class=\"language-swift\">var animator: UIDynamicAnimator!\nvar gravity: UIGravityBehavior!\nvar motion: CMMotionManager!\n\nvar queue: OperationQueue! \/\/ used for updating UI objects with motion<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">and initialize in <strong>viewDidLoad<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"swift\" class=\"language-swift\">queue = OperationQueue.current\nanimator = UIDynamicAnimator(referenceView: self.view)\ngravity = UIGravityBehavior(items: [label1, label2, button1, button2, switch1, switch2])\nmotion = CMMotionManager()<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">What happen here is that we have created a <em>background queue<\/em>, we have attached the <em>animator the the view container<\/em>, attached to the <em>gravity object the elements<\/em> you want to move and <em>instantiate a motion manager<\/em>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The last step is to add to the animator the gravity behavior:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"swift\" class=\"language-swift\">animator.addBehaviour(gravity)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Your code should be like this, you can build and run &#x25b6;&#xfe0f; on a physical device to see the effect:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"swift\" class=\"language-swift\">override func viewDidLoad() {\n    super.viewDidLoad()\n        \n    queue = OperationQueue.current\n    animator = UIDynamicAnimator(referenceView: self.view)\n    gravity = UIGravityBehavior(items: [label1, label2, button1, button2, switch1, switch2])\n    motion = CMMotionManager()\n        \n    animator.addBehavior(gravity)\n}<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-video\"><video height=\"1920\" style=\"aspect-ratio: 886 \/ 1920;\" width=\"886\" autoplay loop muted src=\"http:\/\/www.albertopasca.it\/whiletrue\/wp-content\/uploads\/2021\/11\/RPReplay_Final1637241400.mov\"><\/video><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Cool, but without borders, the UI components fall outside your device&#8230; &#x1f631;&#x1f631;&#x1f631;<\/p>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-background has-vivid-red-background-color has-vivid-red-color\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Add boundaries &#x1f91f;:<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In order to avoid the falling out of objects you need to define a border and the objects that collides.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"swift\" class=\"language-swift\">\/\/ the objects that responds to collision\nlet collision = UICollisionBehavior(items: [label1, label2, button1, button2, switch1, switch2])\n\n\/\/ the boundary AKA the borders. In this case if the full ViewController view\ncollision.addBoundary(withIdentifier: \"borders\" as NSCopying, for: UIBezierPath(rect: self.view.frame))<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Now you can add the <strong>CollisionBehaviour<\/strong> to your animation:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"swift\" class=\"language-swift\">animator.addBehavior(collision)<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-background has-vivid-green-cyan-background-color has-vivid-green-cyan-color\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Move objects using device motion &#x1f3ce;:<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">You can use CoreMotion SDK to detect device movements and move objects around respecting always the gravity.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To do this, you can implement the <strong>startDeviceMotionUpdates<\/strong> of the motion manager:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"swift\" class=\"language-swift\">motion.startDeviceMotionUpdates(to: queue) { motion, error in\n    guard let motion = motion else { return }\n\n    let grav: CMAcceleration = motion.gravity\n    let x = CGFloat(grav.x)\n    let y = CGFloat(grav.y)\n    var p = CGPoint(x: x, y: y)\n\n    if let orientation = UIApplication.shared.windows.first(where: { $0.isKeyWindow })?.windowScene?.interfaceOrientation {\n        if orientation == .landscapeLeft {\n            let t = p.x\n            p.x = 0 - p.y\n            p.y = t\n        } else if orientation == .landscapeRight {\n            let t = p.x\n            p.x = p.y\n            p.y = 0 - t\n        } else if orientation == .portraitUpsideDown {\n            p.x *= -1\n            p.y *= -1\n        }\n    }\n\n    let v = CGVector(dx: p.x, dy: 0 - p.y)\n    self.gravity.gravityDirection = v\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-background has-vivid-purple-background-color has-vivid-purple-color\"\/>\n\n\n\n<p class=\"wp-block-paragraph\">&#x1f389; <strong>Your code is now complete!<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Build and run &#x25b6;&#xfe0f; to see the effects like the first attached video!<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"805\" src=\"https:\/\/i0.wp.com\/www.albertopasca.it\/whiletrue\/wp-content\/uploads\/2021\/11\/IMG_9113.jpg?resize=1024%2C805\" alt=\"\" class=\"wp-image-2999\" srcset=\"https:\/\/i0.wp.com\/www.albertopasca.it\/whiletrue\/wp-content\/uploads\/2021\/11\/IMG_9113.jpg?resize=1024%2C805 1024w, https:\/\/i0.wp.com\/www.albertopasca.it\/whiletrue\/wp-content\/uploads\/2021\/11\/IMG_9113.jpg?resize=300%2C236 300w, https:\/\/i0.wp.com\/www.albertopasca.it\/whiletrue\/wp-content\/uploads\/2021\/11\/IMG_9113.jpg?resize=768%2C603 768w, https:\/\/i0.wp.com\/www.albertopasca.it\/whiletrue\/wp-content\/uploads\/2021\/11\/IMG_9113.jpg?w=1125 1125w\" sizes=\"auto, (max-width: 1000px) 100vw, 1000px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Too lazy for following all the steps in this tutorial? You can import directly the <em>Swift Package<\/em> into Xcode: <strong><a href=\"https:\/\/github.com\/elpsk\/Gravity\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/github.com\/elpsk\/Gravity<\/a> <\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Have fun and enjoy gravity! &#x2764;&#xfe0f;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today a funny &#x1f60e; way to use device motion to move using the gravity the objects on your view controller, like this:<\/p>\n","protected":false},"author":1,"featured_media":2999,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_hide_featured_image":false,"_jetpack_featured_image_duplicate_dismissed":0,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"#Swift - Have fun with GravityBehavior and DynamicAnimator #iOS #gravity #funny","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[1],"tags":[1015,15,22,87,594,46,19,14,18],"class_list":["post-2995","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-generic","tag-cmacceleration","tag-cmmotionmanager","tag-gravity","tag-ios","tag-motion","tag-swift","tag-uicollisionbehavior","tag-uidynamicanimator","tag-uigravitybehavior"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Swift - Have fun with GravityBehavior and DynamicAnimator - while (true) {\u00a0}<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/www.albertopasca.it\/whiletrue\/swift-have-fun-with-gravitybehavior-and-dynamicanimator\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Swift - Have fun with GravityBehavior and DynamicAnimator - while (true) {\u00a0}\" \/>\n<meta property=\"og:description\" content=\"Today a funny &#x1f60e; way to use device motion to move using the gravity the objects on your view controller, like this:\" \/>\n<meta property=\"og:url\" content=\"http:\/\/www.albertopasca.it\/whiletrue\/swift-have-fun-with-gravitybehavior-and-dynamicanimator\/\" \/>\n<meta property=\"og:site_name\" content=\"while (true) {\u00a0}\" \/>\n<meta property=\"article:published_time\" content=\"2021-11-19T08:00:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-11-27T15:42:12+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/i0.wp.com\/www.albertopasca.it\/whiletrue\/wp-content\/uploads\/2021\/11\/IMG_9113.jpg?fit=1125%2C884\" \/>\n\t<meta property=\"og:image:width\" content=\"1125\" \/>\n\t<meta property=\"og:image:height\" content=\"884\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Alberto Pasca\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@lupasca\" \/>\n<meta name=\"twitter:site\" content=\"@lupasca\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Alberto Pasca\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"http:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/swift-have-fun-with-gravitybehavior-and-dynamicanimator\\\/#article\",\"isPartOf\":{\"@id\":\"http:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/swift-have-fun-with-gravitybehavior-and-dynamicanimator\\\/\"},\"author\":{\"name\":\"Alberto Pasca\",\"@id\":\"http:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/#\\\/schema\\\/person\\\/d3cf0df774eb4aac797f246a795e9fac\"},\"headline\":\"Swift &#8211; Have fun with GravityBehavior and DynamicAnimator\",\"datePublished\":\"2021-11-19T08:00:00+00:00\",\"dateModified\":\"2021-11-27T15:42:12+00:00\",\"mainEntityOfPage\":{\"@id\":\"http:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/swift-have-fun-with-gravitybehavior-and-dynamicanimator\\\/\"},\"wordCount\":305,\"publisher\":{\"@id\":\"http:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/#\\\/schema\\\/person\\\/d3cf0df774eb4aac797f246a795e9fac\"},\"image\":{\"@id\":\"http:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/swift-have-fun-with-gravitybehavior-and-dynamicanimator\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/i0.wp.com\\\/www.albertopasca.it\\\/whiletrue\\\/wp-content\\\/uploads\\\/2021\\\/11\\\/IMG_9113.jpg?fit=1125%2C884\",\"keywords\":[\"CMAcceleration\",\"CMMotionManager\",\"gravity\",\"iOS\",\"motion\",\"swift\",\"UICollisionBehavior\",\"UIDynamicAnimator\",\"UIGravityBehavior\"],\"articleSection\":[\"generic\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"http:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/swift-have-fun-with-gravitybehavior-and-dynamicanimator\\\/\",\"url\":\"http:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/swift-have-fun-with-gravitybehavior-and-dynamicanimator\\\/\",\"name\":\"Swift - Have fun with GravityBehavior and DynamicAnimator - while (true) {\u00a0}\",\"isPartOf\":{\"@id\":\"http:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"http:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/swift-have-fun-with-gravitybehavior-and-dynamicanimator\\\/#primaryimage\"},\"image\":{\"@id\":\"http:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/swift-have-fun-with-gravitybehavior-and-dynamicanimator\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/i0.wp.com\\\/www.albertopasca.it\\\/whiletrue\\\/wp-content\\\/uploads\\\/2021\\\/11\\\/IMG_9113.jpg?fit=1125%2C884\",\"datePublished\":\"2021-11-19T08:00:00+00:00\",\"dateModified\":\"2021-11-27T15:42:12+00:00\",\"breadcrumb\":{\"@id\":\"http:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/swift-have-fun-with-gravitybehavior-and-dynamicanimator\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/swift-have-fun-with-gravitybehavior-and-dynamicanimator\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/swift-have-fun-with-gravitybehavior-and-dynamicanimator\\\/#primaryimage\",\"url\":\"https:\\\/\\\/i0.wp.com\\\/www.albertopasca.it\\\/whiletrue\\\/wp-content\\\/uploads\\\/2021\\\/11\\\/IMG_9113.jpg?fit=1125%2C884\",\"contentUrl\":\"https:\\\/\\\/i0.wp.com\\\/www.albertopasca.it\\\/whiletrue\\\/wp-content\\\/uploads\\\/2021\\\/11\\\/IMG_9113.jpg?fit=1125%2C884\",\"width\":1125,\"height\":884,\"caption\":\"swift gravity motion\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/swift-have-fun-with-gravitybehavior-and-dynamicanimator\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Swift &#8211; Have fun with GravityBehavior and DynamicAnimator\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/#website\",\"url\":\"http:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/\",\"name\":\"while (true) {\u00a0}\",\"description\":\"notes from a developer, tutorials, how-to, ideas\",\"publisher\":{\"@id\":\"http:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/#\\\/schema\\\/person\\\/d3cf0df774eb4aac797f246a795e9fac\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"http:\\\/\\\/www.albertopasca.it\\\/whiletrue\\\/#\\\/schema\\\/person\\\/d3cf0df774eb4aac797f246a795e9fac\",\"name\":\"Alberto Pasca\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4d8fe43c412d0bb34a71c41f60dce53eff3afc027853cbfd20bee6ddc7e93f35?s=96&d=retro&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4d8fe43c412d0bb34a71c41f60dce53eff3afc027853cbfd20bee6ddc7e93f35?s=96&d=retro&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4d8fe43c412d0bb34a71c41f60dce53eff3afc027853cbfd20bee6ddc7e93f35?s=96&d=retro&r=g\",\"caption\":\"Alberto Pasca\"},\"logo\":{\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4d8fe43c412d0bb34a71c41f60dce53eff3afc027853cbfd20bee6ddc7e93f35?s=96&d=retro&r=g\"},\"description\":\"Software engineer @ Pirelli &amp; C. S.p.A. with a strong passion for mobile \uf8ff development, security, and connected things.\",\"sameAs\":[\"https:\\\/\\\/www.albertopasca.it\",\"albertopasca\",\"https:\\\/\\\/x.com\\\/lupasca\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Swift - Have fun with GravityBehavior and DynamicAnimator - while (true) {\u00a0}","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"http:\/\/www.albertopasca.it\/whiletrue\/swift-have-fun-with-gravitybehavior-and-dynamicanimator\/","og_locale":"en_US","og_type":"article","og_title":"Swift - Have fun with GravityBehavior and DynamicAnimator - while (true) {\u00a0}","og_description":"Today a funny &#x1f60e; way to use device motion to move using the gravity the objects on your view controller, like this:","og_url":"http:\/\/www.albertopasca.it\/whiletrue\/swift-have-fun-with-gravitybehavior-and-dynamicanimator\/","og_site_name":"while (true) {\u00a0}","article_published_time":"2021-11-19T08:00:00+00:00","article_modified_time":"2021-11-27T15:42:12+00:00","og_image":[{"width":1125,"height":884,"url":"https:\/\/i0.wp.com\/www.albertopasca.it\/whiletrue\/wp-content\/uploads\/2021\/11\/IMG_9113.jpg?fit=1125%2C884","type":"image\/jpeg"}],"author":"Alberto Pasca","twitter_card":"summary_large_image","twitter_creator":"@lupasca","twitter_site":"@lupasca","twitter_misc":{"Written by":"Alberto Pasca","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"http:\/\/www.albertopasca.it\/whiletrue\/swift-have-fun-with-gravitybehavior-and-dynamicanimator\/#article","isPartOf":{"@id":"http:\/\/www.albertopasca.it\/whiletrue\/swift-have-fun-with-gravitybehavior-and-dynamicanimator\/"},"author":{"name":"Alberto Pasca","@id":"http:\/\/www.albertopasca.it\/whiletrue\/#\/schema\/person\/d3cf0df774eb4aac797f246a795e9fac"},"headline":"Swift &#8211; Have fun with GravityBehavior and DynamicAnimator","datePublished":"2021-11-19T08:00:00+00:00","dateModified":"2021-11-27T15:42:12+00:00","mainEntityOfPage":{"@id":"http:\/\/www.albertopasca.it\/whiletrue\/swift-have-fun-with-gravitybehavior-and-dynamicanimator\/"},"wordCount":305,"publisher":{"@id":"http:\/\/www.albertopasca.it\/whiletrue\/#\/schema\/person\/d3cf0df774eb4aac797f246a795e9fac"},"image":{"@id":"http:\/\/www.albertopasca.it\/whiletrue\/swift-have-fun-with-gravitybehavior-and-dynamicanimator\/#primaryimage"},"thumbnailUrl":"https:\/\/i0.wp.com\/www.albertopasca.it\/whiletrue\/wp-content\/uploads\/2021\/11\/IMG_9113.jpg?fit=1125%2C884","keywords":["CMAcceleration","CMMotionManager","gravity","iOS","motion","swift","UICollisionBehavior","UIDynamicAnimator","UIGravityBehavior"],"articleSection":["generic"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"http:\/\/www.albertopasca.it\/whiletrue\/swift-have-fun-with-gravitybehavior-and-dynamicanimator\/","url":"http:\/\/www.albertopasca.it\/whiletrue\/swift-have-fun-with-gravitybehavior-and-dynamicanimator\/","name":"Swift - Have fun with GravityBehavior and DynamicAnimator - while (true) {\u00a0}","isPartOf":{"@id":"http:\/\/www.albertopasca.it\/whiletrue\/#website"},"primaryImageOfPage":{"@id":"http:\/\/www.albertopasca.it\/whiletrue\/swift-have-fun-with-gravitybehavior-and-dynamicanimator\/#primaryimage"},"image":{"@id":"http:\/\/www.albertopasca.it\/whiletrue\/swift-have-fun-with-gravitybehavior-and-dynamicanimator\/#primaryimage"},"thumbnailUrl":"https:\/\/i0.wp.com\/www.albertopasca.it\/whiletrue\/wp-content\/uploads\/2021\/11\/IMG_9113.jpg?fit=1125%2C884","datePublished":"2021-11-19T08:00:00+00:00","dateModified":"2021-11-27T15:42:12+00:00","breadcrumb":{"@id":"http:\/\/www.albertopasca.it\/whiletrue\/swift-have-fun-with-gravitybehavior-and-dynamicanimator\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["http:\/\/www.albertopasca.it\/whiletrue\/swift-have-fun-with-gravitybehavior-and-dynamicanimator\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/www.albertopasca.it\/whiletrue\/swift-have-fun-with-gravitybehavior-and-dynamicanimator\/#primaryimage","url":"https:\/\/i0.wp.com\/www.albertopasca.it\/whiletrue\/wp-content\/uploads\/2021\/11\/IMG_9113.jpg?fit=1125%2C884","contentUrl":"https:\/\/i0.wp.com\/www.albertopasca.it\/whiletrue\/wp-content\/uploads\/2021\/11\/IMG_9113.jpg?fit=1125%2C884","width":1125,"height":884,"caption":"swift gravity motion"},{"@type":"BreadcrumbList","@id":"http:\/\/www.albertopasca.it\/whiletrue\/swift-have-fun-with-gravitybehavior-and-dynamicanimator\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/www.albertopasca.it\/whiletrue\/"},{"@type":"ListItem","position":2,"name":"Swift &#8211; Have fun with GravityBehavior and DynamicAnimator"}]},{"@type":"WebSite","@id":"http:\/\/www.albertopasca.it\/whiletrue\/#website","url":"http:\/\/www.albertopasca.it\/whiletrue\/","name":"while (true) {\u00a0}","description":"notes from a developer, tutorials, how-to, ideas","publisher":{"@id":"http:\/\/www.albertopasca.it\/whiletrue\/#\/schema\/person\/d3cf0df774eb4aac797f246a795e9fac"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/www.albertopasca.it\/whiletrue\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"http:\/\/www.albertopasca.it\/whiletrue\/#\/schema\/person\/d3cf0df774eb4aac797f246a795e9fac","name":"Alberto Pasca","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4d8fe43c412d0bb34a71c41f60dce53eff3afc027853cbfd20bee6ddc7e93f35?s=96&d=retro&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4d8fe43c412d0bb34a71c41f60dce53eff3afc027853cbfd20bee6ddc7e93f35?s=96&d=retro&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4d8fe43c412d0bb34a71c41f60dce53eff3afc027853cbfd20bee6ddc7e93f35?s=96&d=retro&r=g","caption":"Alberto Pasca"},"logo":{"@id":"https:\/\/secure.gravatar.com\/avatar\/4d8fe43c412d0bb34a71c41f60dce53eff3afc027853cbfd20bee6ddc7e93f35?s=96&d=retro&r=g"},"description":"Software engineer @ Pirelli &amp; C. S.p.A. with a strong passion for mobile \uf8ff development, security, and connected things.","sameAs":["https:\/\/www.albertopasca.it","albertopasca","https:\/\/x.com\/lupasca"]}]}},"jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"jetpack-related-posts":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/www.albertopasca.it\/whiletrue\/wp-content\/uploads\/2021\/11\/IMG_9113.jpg?fit=1125%2C884","_links":{"self":[{"href":"http:\/\/www.albertopasca.it\/whiletrue\/wp-json\/wp\/v2\/posts\/2995","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.albertopasca.it\/whiletrue\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.albertopasca.it\/whiletrue\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.albertopasca.it\/whiletrue\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.albertopasca.it\/whiletrue\/wp-json\/wp\/v2\/comments?post=2995"}],"version-history":[{"count":5,"href":"http:\/\/www.albertopasca.it\/whiletrue\/wp-json\/wp\/v2\/posts\/2995\/revisions"}],"predecessor-version":[{"id":3081,"href":"http:\/\/www.albertopasca.it\/whiletrue\/wp-json\/wp\/v2\/posts\/2995\/revisions\/3081"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.albertopasca.it\/whiletrue\/wp-json\/wp\/v2\/media\/2999"}],"wp:attachment":[{"href":"http:\/\/www.albertopasca.it\/whiletrue\/wp-json\/wp\/v2\/media?parent=2995"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.albertopasca.it\/whiletrue\/wp-json\/wp\/v2\/categories?post=2995"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.albertopasca.it\/whiletrue\/wp-json\/wp\/v2\/tags?post=2995"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}