您好,欢迎来到知库网。
搜索
您的当前位置:首页如何暂停和恢复CALayer动画

如何暂停和恢复CALayer动画

来源:知库网

Q: How do I pause all animations in a layer tree?

A: In order to pause animations in a layer tree, you can take advantage of the fact that a CALayer conforms to the CAMediaTiming protocol. The CAMediaTiming protocol defines, among other things, a speed with which its timeline progresses, which you can use to pause all animations on the target layer. Listing 1 demonstrates how you can do this.

Listing 1 Pause and Resume animations.

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

Copyright © 2019- zicool.com 版权所有 湘ICP备2023022495号-2

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务