Book Image

Application Development in iOS 7

By : Kyle Begeman
Book Image

Application Development in iOS 7

By: Kyle Begeman

Overview of this book

Table of Contents (15 chapters)
Application Development in iOS 7
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Snapping items


Our last behavior we will cover in this book is the UISnapBehavior class. UIKit Dynamics provides a built-in behavior that will snap an item from its starting point to a specified end point with built-in damping. Let's have our food image view snap from the top of the screen into its final position.

Scroll to viewDidLoad and remove all of our gravity and collision code (keep our animator). Add the following code to viewDidLoad:

UISnapBehavior *snapBehaviour = [[UISnapBehavior alloc] initWithItem:self.foodImageView snapToPoint:CGPointMake(160, 202)];
snapBehaviour.damping = 0.65f;
[animator addBehavior:snapBehaviour];

Here we allocate new UISnapBehavior and init options with our food image view. We also pass the point we want the item to snap to, in this case, the final position of the image view. We set the damping value to be a bit higher to give a milder spring effect (the lower the number, the more springy the item will be).

The last thing to do is to change the starting point...