Graphics Reference
In-Depth Information
Canceling an Animation in Progress
As mentioned earlier in this chapter, you can use the key parameter of the
-addAnimation:forKey: method to retrieve an animation after it has been added to a layer
by using the following method:
- ( CAAnimation *)animationForKey:( NSString *)key;
Modifying animations once they are in progress is not supported, so the primary uses of this
are to check what the animation properties are or to detect whether a particular animation is
attached to the layer.
To terminate a specific animation, you can remove it from the layer using the following
method:
- (void)removeAnimationForKey:( NSString *)key;
Or you can just remove all animations using this method:
- (void)removeAllAnimations;
As soon as an animation is removed, the layer appearance updates to match its current
model value. Animations are removed automatically when they finish unless you specify
that they shouldn't by setting the removedOnCompletion property of the animation to NO . If
you set the animation to not be removed automatically, it is important to remove it yourself
when it is no longer needed; otherwise, it will stay in memory until the layer itself is
eventually destroyed.
Let's extend our rotating ship example once again with buttons to stop and start the
animation. This time, we provide a non- nil value for our animation key so that we can
remove it later. The flag argument in the -animationDidStop:finished: method
indicates whether the animation finished naturally or was interrupted, which we log in the
console. If you terminate the animation using the stop button, it will log NO , but if you allow
it to complete, it will log YES .
See Listing 8.15 for the updated example code. Figure 8.6 shows it in action.
Listing 8.15 Starting and Stopping an Animation
@interface ViewController ()
@property ( nonatomic , weak ) IBOutlet UIView *containerView;
@property ( nonatomic , strong ) CALayer *shipLayer;
@end
 
Search WWH ::




Custom Search