2.5. Adding a Dynamic Snap Effect to Your UI Components
Problem
Using an animation, you want to snap a view in your UI to a specific point on the screen, with the elasticity of a real-world snap effect. This means that when your UI component snaps to the given point, you will feel that it has elasticity built into it.
Solution
Instantiate an object of type UISnapBehavior
and add it to an animator of
type UIDynamicAnimator
.
Discussion
To really understand how the snap dynamic behavior works, think
about a small amount of jelly covered in oil with a string attached to
it, sitting on a very smooth table. I know that sentence sounds really
odd. But bear with me. Now imagine from another point on the table,
pulling on that string to get the jelly to move from its initial point
to the point you ordered it to move to. With the oil all around it, the
jelly will move smoothly from that point to where you want it to go, and
because it is jelly, it will wiggle when it snaps to position. This
behavior is exactly what you can achieve with the UISnapBehavior
class.
One of the use cases for this is when you have an app and some views on the screen, such as images, and you want the user to be able to dictate where those views have to be moved to create a customized UI for the user. One way of handling this is using the technique that we learned in Recipe 2.3, but that solution is quite rigid and has its own use cases. Here in this recipe, we have a view on our screen, and we want to allow the user to ...
Get iOS 7 Programming Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.