O'Reilly logo

iOS 7 Programming Cookbook by Vandad Nahavandipoor

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

2.3. Animating Your UI Components with a Push

Problem

You want to “flick” your views from one point to another.

Solution

Initialize a behavior object of type UIPushBehavior using its initWithItems:mode: method, and for the mode, pass the value of UIPushBehaviorModeContinuous. Once you are ready to start pushing the items toward an angle, issue the setAngle: method on the push behavior to set the angle (in radians) for the behavior. After that, you will need to set the magnitude, or the force behind the push. You can set this force using the setMagnitude: method of the push behavior. The magnitude is calculated in this way: each magnitude of 1 point will result in acceleration of 100 points per second squared for your target views.

Discussion

Push behaviors, especially continuous pushes, are very useful. Let’s say you are working on a scrapbook iPad app, and on top of the screen, you have created three slides, each representing one of the scrapbook pages that the user has created. On the bottom of the screen, you have various pictures that the user can drag and drop into the pages. One way to allow the user to do this is to add a tap gesture recognizer (see Recipe 10.5) to your reference view to track the tap and allow the pictures to be moved onto the target slide, which will, in turn, simulate the dragging. The other, and perhaps better, way of doing this is to use the push behavior that Apple has introduced into UIKit.

The push behavior is of type UIPushBehavior and has a magnitude ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required