Cancel the animation (explicit animation) and cancel the animation
Cancel an animation during the animation process
As mentioned before, you can use-addAnimation:forKey:
Methodkey
Parameters are used to retrieve an animation after an animation is added. The following method is used:
- (CAAnimation *)animationForKey:(NSString *)key;
However, this method does not support modifying the animation during the animation operation. Therefore, this method is mainly used to detect the animation attributes or determine whether the animation is added to the current layer.
To terminate a specified animation, you can remove it from the layer using the following method:
- (void)removeAnimationForKey:(NSString *)key;
Or remove all animations:
- (void)removeAllAnimations;
Once the animation is removed, the appearance of the layer is immediately updated to the value of the current model layer. Generally, an animation is automatically removed after the animation ends, unless it is setremovedOnCompletion
IsNO
If the animation is not automatically removed after the animation is set, you need to manually remove it when it is not needed; otherwise, it will remain in the memory until the layer is destroyed.
Let's expand the previous rotating ship example. Here we add a button to stop or start the animation. This time we use a non-nil
As the animation key, so that you can remove it later.-animationDidStop:finished:
Methodflag
The parameter table shows whether the animation ends naturally or is interrupted. You can print it on the console. If you use the stop button to terminate the animation, it printsNO
If it is allowed to complete, it will printYES
.
Listing 8.15 is the updated sample code, and Figure 8.6 shows the result.
Listing 8.15 starts and stops an animation
1 @interface ViewController () 2 3 @property (nonatomic, weak) IBOutlet UIView *containerView; 4 @property (nonatomic, strong) CALayer *shipLayer; 5 6 @end 7 8 @implementation ViewController 9 10 - (void)viewDidLoad11 {12 [super viewDidLoad];13 //add the ship14 self.shipLayer = [CALayer layer];15 self.shipLayer.frame = CGRectMake(0, 0, 128, 128);16 self.shipLayer.position = CGPointMake(150, 150);17 self.shipLayer.contents = (__bridge id)[UIImage imageNamed: @"Ship.png"].CGImage;18 [self.containerView.layer addSublayer:self.shipLayer];19 }20 21 - (IBAction)start22 {23 //animate the ship rotation24 CABasicAnimation *animation = [CABasicAnimation animation];25 animation.keyPath = @"transform.rotation";26 animation.duration = 2.0;27 animation.byValue = @(M_PI * 2);28 animation.delegate = self;29 [self.shipLayer addAnimation:animation forKey:@"rotateAnimation"];30 }31 32 - (IBAction)stop33 {34 [self.shipLayer removeAnimationForKey:@"rotateAnimation"];35 }36 37 - (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag38 {39 //log that the animation stopped40 NSLog(@"The animation stopped (finished: %@)", flag? @"YES": @"NO");41 }42 43 @end
View Code
Figure 8.6 rotation animation controlled by the start and stop buttons