{"id":2168,"date":"2017-05-21T00:41:39","date_gmt":"2017-05-21T04:41:39","guid":{"rendered":"http:\/\/www.rexfeng.com\/blog\/?p=2168"},"modified":"2017-05-21T00:41:39","modified_gmt":"2017-05-21T04:41:39","slug":"multiple-uidynamicanimators","status":"publish","type":"post","link":"https:\/\/www.rexfeng.com\/blog\/2017\/05\/multiple-uidynamicanimators\/","title":{"rendered":"Multiple UIDynamicAnimators"},"content":{"rendered":"<p>In past apps, I tended to have one\u00a0<a href=\"https:\/\/developer.apple.com\/reference\/uikit\/uidynamicanimator\">UIDynamicAnimator<\/a> in my ViewController and that was that.\u00a0UIDynamicAnimator allows you to use\u00a0<a href=\"https:\/\/www.bignerdranch.com\/blog\/uidynamics-in-swift\/\">UIDynamics<\/a> \/ effects on your UIViews.<\/p>\n<p>The issue that I ran into was that\u00a0<a href=\"https:\/\/developer.apple.com\/reference\/uikit\/uidynamicanimator\/1621201-removebehavior\">removeBehavior(_:)<\/a>, which &#8220;<em>Removes a specified dynamic behavior from a dynamic animator<\/em>&#8220;, didn&#8217;t seem to work. I would keep track of specific\u00a0UIDynamicBehavior instances and pass them as the argument for\u00a0removeBehavior(_:)\u00a0but it didn&#8217;t appear to remove the behavior.<\/p>\n<p>What does work is calling\u00a0<a href=\"https:\/\/developer.apple.com\/reference\/uikit\/uidynamicanimator\/1621197-removeallbehaviors\">removeAllBehaviors()<\/a> on the UIDynamicAnimator. This is fine if you only have one UIView. But most likely, you have multiple UIViews &amp; behaviors. Calling remove all on the only animator isn&#8217;t a good idea. That could leave UIViews frozen out of place.<\/p>\n<p>Recently, I released a fun weekend app, <a href=\"https:\/\/itunes.apple.com\/us\/app\/fun-faces-silly-flinger\/id1237058922?mt=8\">Fun Faces<\/a>. Browsing stack overflow, it occurred to me to use multiple\u00a0UIDynamicAnimators. One for each UIView I wanted to animate. This worked for my use case, where calling\u00a0removeAllBehaviors() doesn&#8217;t interrupt the other UIView&#8217;s behaviors (if any).<\/p>\n<p>Using multiple\u00a0UIDynamicAnimators isn&#8217;t an answer if you have multiple UIViews under the same animator with\u00a0<a href=\"https:\/\/developer.apple.com\/reference\/uikit\/uicollisionbehavior\">UICollisionBehavior<\/a> or other effects that let the UIViews interact with each other.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In past apps, I tended to have one\u00a0UIDynamicAnimator in my ViewController and that was that.\u00a0UIDynamicAnimator allows you to use\u00a0UIDynamics \/ effects on your UIViews. The issue that I ran into was that\u00a0removeBehavior(_:), which &#8220;Removes a specified dynamic behavior from a dynamic animator&#8220;, didn&#8217;t seem to work. I would keep track of specific\u00a0UIDynamicBehavior instances and pass [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1029],"tags":[380,966,1174,1325,1086],"class_list":["post-2168","post","type-post","status-publish","format-standard","hentry","category-programming","tag-apple","tag-ios","tag-swift","tag-uidynamicanimator","tag-xcode"],"_links":{"self":[{"href":"https:\/\/www.rexfeng.com\/blog\/wp-json\/wp\/v2\/posts\/2168","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rexfeng.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rexfeng.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rexfeng.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rexfeng.com\/blog\/wp-json\/wp\/v2\/comments?post=2168"}],"version-history":[{"count":2,"href":"https:\/\/www.rexfeng.com\/blog\/wp-json\/wp\/v2\/posts\/2168\/revisions"}],"predecessor-version":[{"id":2170,"href":"https:\/\/www.rexfeng.com\/blog\/wp-json\/wp\/v2\/posts\/2168\/revisions\/2170"}],"wp:attachment":[{"href":"https:\/\/www.rexfeng.com\/blog\/wp-json\/wp\/v2\/media?parent=2168"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rexfeng.com\/blog\/wp-json\/wp\/v2\/categories?post=2168"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rexfeng.com\/blog\/wp-json\/wp\/v2\/tags?post=2168"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}