zoukankan      html  css  js  c++  java
  • iOS动画暂停和继续-本质是速度控制和时间坐标转换

    时间永不停止!

    写一个CALayer的分类,控制动画的暂停与继续

    extension CALayer {
        ///暂停动画
        func pauseAnimation() {
            //取出当前时间,转成动画暂停的时间
            let pausedTime = self.convertTime(CACurrentMediaTime(), from: nil)
            //设置动画运行速度为0
            self.speed = 0.0;
            //设置动画的时间偏移量,指定时间偏移量的目的是让动画定格在该时间点的位置
            self.timeOffset = pausedTime
        }
        ///恢复动画
        func resumeAnimation() {
            //获取暂停的时间差
            let pausedTime = self.timeOffset
            self.speed = 1.0
            self.timeOffset = 0.0
            self.beginTime = 0.0
            //用现在的时间减去时间差,就是之前暂停的时间,从之前暂停的时间开始动画
            let timeSincePause = self.convertTime(CACurrentMediaTime(), from: nil) - pausedTime
            self.beginTime = timeSincePause
        }
    }
    



    作者:losedMemory
    链接:https://www.jianshu.com/p/2a3c70a3d350
    來源:简书
    简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

  • 相关阅读:
    [Next] 六.next的优化
    [Next] 五.next自定义内容
    Mac解决端口占用
    [Next] 四.在next中引入redux
    [Next] 服务端渲染知识补充
    [Next] 三.next自定义服务器和路由
    哪些使用UDP、TCP、IP协议
    IDEA配置git
    ssm整合配置
    git传输远程仓库
  • 原文地址:https://www.cnblogs.com/feng9exe/p/10342680.html
Copyright © 2011-2022 走看看