Graphics Reference
In-Depth Information
A simpler and less-destructive approach is to make use of the hierarchical nature of
CAMediaTiming and pause the layer itself. If you set the layer speed to zero, it will pause
any animations that are attached to that layer. Similarly, setting speed to a value greater
than 1.0 will “fast-forward,” and setting a negative speed will “rewind” the animation.
By increasing the layer speed for the main window in your app, you can actually speed up
animations across the entire application. This can prove useful for something like UI
automation, where the tests can be made to run faster if you speed up all the view
transitions. (Note that views that are displayed outside of the main window—such as
UIAlertView —will not be affected.) Try adding the following line to your app delegate to
see this in action:
self.window.layer.speed = 100;
You can also slow down animations across the app in this way, but this is less useful since
it's already possible to slow down animations in the iOS Simulator by using the Toggle
Slow Animations option in the Debug menu.
Manual Animation
A really interesting feature of the timeOffset property is that it enables you to manually
scrub through an animation. By setting speed to zero, you can disable the automatic
playback of an animation and then use the timeOffset to move back and forth through the
animation sequence. This can be a nifty way to allow the user to manipulate an animated
user interface element using gestures.
We'll try a simple example first: Starting with the swinging door animation from earlier in
the chapter, let's modify the code so that the animation is controlled using a finger gesture.
We do this by attaching a UIPanGestureRecognizer to our view and then using it to set
the timeOffset by swiping left and right.
Because we cannot modify the animation after it has been added to the layer, what we will
do instead is pause and adjust the timeOffset value for the layer , which in this case has the
same effect as if we were manipulating the animation directly (see Listing 9.4).
Listing 9.4 Manually Driving an Animation Using Touch Gestures
@interface ViewController ()
@property ( nonatomic , weak ) UIView *containerView;
@property ( nonatomic , strong ) CALayer *doorLayer;
@end
@implementation ViewController
- ( void )viewDidLoad
 
Search WWH ::




Custom Search