Cancel the animation (explicit animation) and cancel the animation

Source: Internet
Author: User

Cancel the animation (explicit animation) and cancel the animation
Cancel an animation during the animation process

As mentioned before, you can use-addAnimation:forKey:MethodkeyParameters 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 setremovedOnCompletionIsNOIf 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-nilAs the animation key, so that you can remove it later.-animationDidStop:finished:MethodflagThe 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 printsNOIf 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

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.