Pause, resume, and end callback of the CALayer animation

Source: Internet
Author: User

Pause, resume, and end callback of the CALayer animation

The source code is as follows:

//// ViewController. m // AnimationLineView /// Created by YouXianMing on 14/11/7. // Copyright (c) 2014 YouXianMing. all rights reserved. // # import "ViewController. h "# import" YXGCD. h "@ interface ViewController () @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; // layer CALayer * layer = [CALayer layer]; layer. frame = CGRectMake (100,100, 3, 3); layer. backgroundColor = [UIColor redColor]. CGColor; layer. delegate = self; [self. view. layer addSublayer: layer]; // animated CABasicAnimation * animation = [CABasicAnimation animationWithKeyPath: @ "bounds"]; animation. fromValue = [NSValue valueWithCGRect: layer. frame]; animation. toValue = [NSValue valueWithCGRect: CGRectMake (100,100,100, 3)]; animation. duration = 3.f; animation. delegate = self; // Set the proxy [layer addAnimation: animation forKey: @ "YouXianMing"]; // add an animation // pause the animation [GCDQueue executeInMainQueue: ^ {[self pauseLayer: layer];} afterDelaySecs: 1.5f]; // restore the animation [GCDQueue executeInMainQueue: ^ {[self resumeLayer: layer];} afterDelaySecs: 3.f]; // remove the animation (based on the key value) [GCDQueue executeInMainQueue: ^ {[layer removeAnimationForKey: @ "YouXianMing"];} afterDelaySecs: 3.5f];} /*** the animation does stop ** @ param anim CAAnimation object * @ param flag is normal removal */-(void) animationDidStop :( CAAnimation *) anim finished :( BOOL) flag {NSLog (@ "animation: % @ whether the animation has been removed midway through: % d", anim, flag );} /*** pause ** @ param layer the stopped layer */-(void) pauseLayer :( CALayer *) layer {CFTimeInterval pausedTime = [layer convertTime: CACurrentMediaTime () fromLayer: nil]; layer. speed = 0.0; layer. timeOffset = pausedTime;}/*** restore ** @ param layer the recovered layer */-(void) resumeLayer :( CALayer *) layer {CFTimeInterval pausedTime = [layer timeOffset]; layer. speed = 1.0; layer. timeOffset = 0.0; layer. beginTime = 0.0; CFTimeInterval timeSincePause = [layer convertTime: CACurrentMediaTime () fromLayer: nil]-pausedTime; layer. beginTime = timeSincePause;} @ end

Note the following:

 

Pause, resume, and end callback of the CALayer animation

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.