zoukankan      html  css  js  c++  java
  • 暂停、恢复CALayer 动画

    CALayer都是实现了CAMediaTiming协议(或者叫做接口)。所以layer的动画有一个很有意思的属性speed。如果一个layer的动画速度变成0.0的时候,很显然这个动画就不再动了。设置layer的speed为0时,layer的动画暂停。speed属性设置为任意大于0的值时,动画回复。

    要暂停一个动画时:

    -(void)pauseLayer:(CALayer*)layer {
       CFTimeInterval pausedTime = [layer convertTime:CACurrentMediaTime() fromLayer:nil];
       layer.speed = 0.0;
       layer.timeOffset = pausedTime;
    }

    恢复一个动画:

    -(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;
    }

    比如你有这么一个在ImageView上执行的动画,你想让这个动画开始执行后点击按钮暂停,在点击按钮恢复就可以这样:

    CABasicAnimation *sizeAnim = [CABasicAnimation animationWithKeyPath:@"transform"];
            sizeAnim.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
            sizeAnim.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(1.5f, 1.5f, 1.f)];
            sizeAnim.duration = .2f;
            sizeAnim.fillMode = kCAFillModeForwards;
            sizeAnim.removedOnCompletion = YES;
            sizeAnim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
            [self.collectionImageView.layer addAnimation:sizeAnim forKey:@"size"];
            
            CAKeyframeAnimation *shakeAnim = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation.z"];
            shakeAnim.values = @[@(M_PI/6), @(-M_PI/6),@(M_PI/6)/*, @(-M_PI/6)*/];
            shakeAnim.timingFunctions = @[[self getTiming:kCAMediaTimingFunctionLinear]
                                          , [self getTiming:kCAMediaTimingFunctionLinear]
                                          ,[self getTiming:kCAMediaTimingFunctionLinear]];
            shakeAnim.duration = .2f;
    //        shakeAnim.beginTime = CACurrentMediaTime() + sizeAnim.duration;
            [self.collectionImageView.layer addAnimation:shakeAnim forKey:@"shake"];

    上面说的ImageView可以通过self.collectionImageView来访问。这里设置的执行时间都不是很长,在实验的时候最好把动画的执行时间设置的长点。

    暂停&恢复的代码:

    static BOOL animationStoped = NO;
    - (IBAction)animationAction:(id)sender {
        if (!animationStoped) {
            [self pauseLayer:self.collectionImageView.layer];
    //        animationStoped = YES;
        }
        else{
            [self resumeLayer:self.collectionImageView.layer];
        }
        animationStoped = !animationStoped;
    }
  • 相关阅读:
    【Python开发】python使用urllib2抓取防爬取链接
    【Python开发】python使用urllib2抓取防爬取链接
    【Python开发】Python之re模块 —— 正则表达式操作
    【Python开发】Python之re模块 —— 正则表达式操作
    【Python开发】Url中文字符时记得转码edcode("utf-8")
    【Python开发】Url中文字符时记得转码edcode("utf-8")
    【Python开发】urllib2异常处理
    【Python开发】urllib2异常处理
    【Python开发】urllib2.urlopen超时问题
    【Python开发】urllib2.urlopen超时问题
  • 原文地址:https://www.cnblogs.com/sunshine-anycall/p/3418685.html
Copyright © 2011-2022 走看看