Layer动画的停止与恢复

animation-pause-resume.gif

CALayerCAAnimation都实现了CAMediaTiming 协议,可以通过CALayer 中实现的协议中的属性来控制动画。
停止动画:通过将speed设置为0,并将timeOffset调整到合适的值。
Apple 文档说明

/* The rate of the layer. Used to scale parent time to local time, e.g. if rate is 2, local time progresses twice as fast as parent time.  Defaults to 1. */

@property float speed;

/* Additional offset in active local time. i.e. to convert from parent time tp to active local time t: t = (tp - begin) * speed + offset.
One use of this is to "pause" a layer by setting `speed' to zero and `offset' to a suitable value. Defaults to 0. */

@property CFTimeInterval timeOffset;

而恢复动画,除了上面两个属性,还需要配合beginTime 属性来让动画在正确的时间、位置恢复。

// 停止动画
- (void)pauseAnimation
{
    CFTimeInterval current = CACurrentMediaTime();
    CFTimeInterval pauseTime = current - layer.beginTime; // layer.beginTime默认为0
    /* 实际此处
      [layer convertTime:CACurrentMediaTime() fromLayer:nil] == current - layer.beginTime == pauseTime
         */
    layer.speed = 0;
    layer.timeOffset = pauseTime;
}

// 恢复动画
- (void)resumeAnimation
{
    CFTimeInterval pauseTime = layer.timeOffset;
    CFTimeInterval timeSincePause = CACurrentMediaTime() - pauseTime;
    /* 相当于
     layer.beginTime = 0 // 先恢复beginTime,确保covert时间正确
     CFTimeInterval pauseTime = [layer convertTime:CACurrentMediaTime() fromLayer:nil]; == 
         */
    layer.speed = 1.0;
    layer.timeOffset = 0.0;
    layer.beginTime = timeSincePause;
}

通过代码可以看出,暂停动画就是将speed设置为0,并且将动画的timeOffset设置到当前时间点。
而恢复动画时,则是恢复speed1,重置timeOffset0,并将beginTime设置为timeSincePause

上图中#A是动画停止的时间点,#B是恢复动画的时间点。从图和代码中变量值的计算和命名可以看出,timeSincePause为动画停止的时间,那为什么要把layerbeginTime设置为动画停止的时间呢?不是应该将开始时间恢复到动画停止的时间才对吗?

这里涉及到CAMediaTiming中时间的一个公式:

t = (tp - beginTime) * speed + offset
t为层上的时间,tp为父图层上的时间
与图层的层级类似,时间也有层级,修改图层上的时间,会影响该图层和子图层,不影响父图层
CoreAnimation有一个全局时间的概念,也就是所谓的马赫时间,它在设备上所有进程都是全局的
图层上的默认时间就是马赫时间,即为CACurrentMediaTime()

在恢复动画时,我们希望的是层上的时间恢复到动画暂停的那个时间,即:

动画停止时,层上时间为pauseTime
恢复动画时,设置了layer.speed = 1, layer.offset = 0
t = pauseTime = (tp - beginTime) * speed + offset
pauseTime = (CACurrentMediaTime() -  beginTime) * 1 + 0
beginTime = CACurrentMediaTime() - pauseTime

可以看出,当beginTime为CACurrentMediaTime() - pauseTime时,层上的时间t是我们期望的pauseTime,这就解释了上面所提出的问题。



对于动画中各个属性的使用效果,可以参照下面的图片理解:
原始动画是一个背景色从橙色到蓝色过度的动画。

Original Animation

BeginTime

这里设置的实际上是beginTime = CACurrentMediaTime() + 1.0,所以动画延迟了1秒开始。这也是设置beginTime的正确方式,像beginTime = 1.0这样的设置,实际上会导致动画不能开始。

BeginTime+fillMode
AutoReverse
RepeatCount
RepeatDuration
RepeatCount+AutoReverse
Speed
TimeOffset

参考:
1. CoreAnimation -- Ljson
2. 控制动画的时间
3. Controlling Animation Timing
4. Comprehend pause and resume animation on a layer
5. 层级关系时间

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥ios动画全貌。在这里你可以看...
    每天刷两次牙阅读 8,613评论 6 30
  • 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥iOS动画全貌。在这里你可以看...
    F麦子阅读 5,161评论 5 13
  • 在iOS实际开发中常用的动画无非是以下四种:UIView动画,核心动画,帧动画,自定义转场动画。 1.UIView...
    请叫我周小帅阅读 3,215评论 1 23
  • Core Animation Core Animation,中文翻译为核心动画,它是一组非常强大的动画处理API,...
    45b645c5912e阅读 3,088评论 0 21
  • 书写的很好,翻译的也棒!感谢译者,感谢感谢! iOS-Core-Animation-Advanced-Techni...
    钱嘘嘘阅读 2,346评论 0 6