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
About the Author
About the Reviewers

Adding gravity

Open FoodDetailViewController.m and add the following instance variables to the view controller:

UIDynamicAnimator* animator;
UIGravityBehavior* gravity;

Scroll to viewDidLoad and add the following code to the bottom of the method:

animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
gravity = [[UIGravityBehavior alloc] initWithItems:@[self.foodImageView]];
    [animator addBehavior:gravity];

Run the application, open the My Foods view, select a food item from the table view, and watch what happens. The food image should start to accelerate towards the bottom of the screen until it eventually falls off the screen, as shown in the following set of screenshots:

Let's go over the code, specifically the two new classes that were just introduced, UIDynamicAnimator and UIGravityBehavior.