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