I'm trying to figure out implement UIKit Dynamics that are similar to those in Jelly's app (specifically swiping down to drag view off-screen).
See the animation: http://vimeo.com/83478484 (@1:17)
I understand how UIKit Dynamics work, but don't have a great physics background and therefore am having trouble combing the different behaviors to get the desired result!
SWIFT 3.0 :
@Rob's answer is great (upvote it!), but I'd remove the manual angular velocity calculations and let UIDynamics do the work with a
UIPushBehavior
. Just set the target offset of theUIPushBehavior
and UIDynamics will do the rotational calculation work for you.Start with @Rob's same setup:
But tweak the gesture recognizer handler to use a
UIPushBehavior
This sort of dragging can be accomplished with an
UIAttachmentBehavior
where you create the attachment behavior uponUIGestureRecognizerStateBegan
, change the anchor uponUIGestureRecognizerStateChanged
. This achieves the dragging with rotation as the user conducts the pan gesture.Upon
UIGestureRecognizerStateEnded
you can remove theUIAttachmentBehavior
, but then apply aUIDynamicItemBehavior
to have the animation seamlessly continue with the same linear and angular velocities the user was dragging it when they let go of it (don't forget to use anaction
block to determine when the view no longer intersects the superview, so you can remove the dynamic behavior and probably the view, too). Or, if your logic determines that you want to return it back to the original location, you can use aUISnapBehavior
to do so.Frankly, on the basis of this short clip, it's a little tough to determine precisely what they're doing, but these are the basic building blocks.
For example, let's assume you create some view you want to drag off the screen:
You can then create a gesture recognizer to drag it off the screen:
That yields (showing both the snap behavior if you don't drag down, as well as the dynamic behavior if you successfully drag it down):
This is only a shell of a demonstration, but it illustrates using a
UIAttachmentBehavior
during the pan gesture, using aUISnapBehavior
if you want to snap it back if you conclude you want to reverse the gesture's animation, but usingUIDynamicItemBehavior
to finish the animation of dragging it down, off the screen, but making the transition from the theUIAttachmentBehavior
to the final animation as smooth as possible. I also added a little gravity at the same time as that finalUIDynamicItemBehavior
so that it smoothly accelerates off the screen (so it doesn't take too long).Customize this as you see fit. Notably, that pan gesture handler is unwieldy enough that I might contemplate creating a custom recognizer to clean up that code. But hopefully this illustrates the basic concepts in using UIKit Dynamics to drag a view off the bottom of the screen.