关于动画暂停和恢复的简单说明

来源:互联网 发布:涂鸦制作软件 编辑:程序博客网 时间:2024/06/06 01:26

第一步的思路:判断动画是否存在 判断的依据是动画标识符translationAnimation

    if ([_imgView.layeranimationForKey:@"translationAnimation"]) {

              if (_imgView.layer.speed == 0) {

            //开始      // 1. CALayer的时间继续行走

            _imgView.layer.speed = 1;

            

            CFTimeInterval pause = [_imgView.layertimeOffset];

            _imgView.layer.timeOffset = 0;

            // 4. 计算暂停的时间(这里也可以用CACurrentMediaTime()-pausedTime)

            CFTimeInterval sinceTime = [_imgView.layerconvertTime:CACurrentMediaTime() fromLayer:nil] - pause;

            // 5. 设置相对于父坐标系的开始时间(往后退timeSincePause)

            _imgView.layer.beginTime = sinceTime - pause;

动画暂停之后又开始在原来的基础上继续转动的效果图

            

               }else {  //暂停

            //     当前的时间   转为  图层暂停的时间

            CFTimeInterval pauseTime = [_imgView.layerconvertTime:CACurrentMediaTime() fromLayer:nil];

            _imgView.layer.speed = 0;

            // CALayer的时间停留在pausedTime这个时刻

动画暂停的效果图

            _imgView.layer.timeOffset = pauseTime;

        }

如果没有动画的话 我们调用下面这个方法让动画开始​

    }else {

        [self_startAnimation];

    }

//开始动画的方法

- (void)_startAnimation {

    //旋转

    CABasicAnimation *anim = [CABasicAnimationanimationWithKeyPath:@"transform.rotation.y"];

    

    anim.toValue = @(M_PI);

    

    //设置动画时间

    anim.duration = 5;

    

    //动画的次数 MAXFLOAT非常大的一个浮点型数字

    anim.repeatCount = MAXFLOAT;

    

    //设置代理

    anim.delegate = self;

    

   

在这里我们设置了动画的标识符  方便我们在判断的时候可以根据这个标识符拿到动画

        [_imgView.layer  addAnimation:anim forKey:@"translationAnimation"];

}

动画刚刚开始是的效果图

0 0
原创粉丝点击