CAMediaTiming / 时间协议

CAMediaTiming / 时间协议

  • repeatCount,动画的重复次数,可以设置为小数。设置为HUGE_VALF,表示无限重复。
  • repeatDuration,动画总时长,如果大于单次时长,则重复;如果小于单次时长,则截断
  • duration单次动画时长。
  • speed图层动画模型相对于父图层CALayer的时间流逝速度。
  • fillMode,有效期结束后,动画对象的呈现效果冻结还是移除
  • beginTime,相对于父对象的开始时间。注意,以系统的绝对时间为准。例如:
/**
当前时间2秒以后开始动画
*/
keyFrameAnim.beginTime = CACurrentMediaTime() + 2;
/**
截止到当前时间,动画已经执行了2秒,
注意,如果执行的时间大于动画时长,则表示动画已经执行过。
*/
keyFrameAnim.beginTime = CACurrentMediaTime() - 2;
  • timeOffset,时间轴偏移量。将时间轴移动至偏移位置,再执行整个动画时长。假设动画时长3秒,偏移量为8,则开始位置为8 % 3 = 2,再执行3秒,即在整个时长的1/ 3处结束。
  • CACurrentMediaTime,返回系统当前的绝对时间(从本次开机开始),单位秒。

 /**
  The receiver does not appear until it begins and is removed from the presentation when it is completed.
  */
 kCAFillModeRemoved; // (默认)动画模型的呈现效果直至开始时才显示,并在动画结束后移除。
 /**
  The receiver does not appear until it begins but remains visible in its final state when it is completed.
  */
 kCAFillModeForwards; // 动画模型的呈现效果直至开始时才显示,但在动画结束后仍然显示最后的状态。
 /**
  The receiver appears in its initial state before it begins but is removed from the presentation when it is completed.
  */
 kCAFillModeBackwards; // 动画开始之前,动画模型显示其初始呈现效果,但在动画结束后移除。
 /**
  The receiver appears in its initial state before it begins and remains visible in its final state when it is completed.
  */
 kCAFillModeBoth; // 动画开始之前,动画模型显示其初始呈现效果,并且在动画结束后仍然显示最后的状态。

暂停/继续动画demo

- (IBAction)pauseBtnClicked:(id)sender {
    /**
     判断当前图层对象是否有针对postion属性的动画效果
     */
    if ([self.layer.presentationLayer animationForKey:@"position"]) {
        // 通过绝对时间获取图层的本地时间
        CFTimeInterval localTime = [self.layer convertTime:CACurrentMediaTime() fromLayer:nil];
        /**
         将图层的时间流逝速度设置为0,以暂停动画
         */
        self.layer.speed = 0;
        // 设置图层的时间轴偏移量,为继续动画做准备
        self.layer.timeOffset = localTime;
        
    }
}

- (IBAction)continueBtnClicked:(id)sender {
    /**
     判断当前图层对象是否有针对postion属性的动画效果
     */
    if ([self.layer.presentationLayer animationForKey:@"position"]) {
        // 获取上次暂停时的时间轴偏移量
        CFTimeInterval timeOffset = self.layer.timeOffset;
        
        // 重置时间轴偏移量
        self.layer.timeOffset = 0;
        // 速度还原为1
        self.layer.speed = 1;
        
        // 重置开始时间
#warning 此处严重不理解。
        self.layer.beginTime = 0;
        
        // 计算暂停时间和当前时间的差值
        CFTimeInterval localTime = [self.layer convertTime:CACurrentMediaTime() fromLayer:nil];
        CFTimeInterval timeSincePause = localTime - timeOffset;
        
        // 从上一次暂停处开始
        self.layer.beginTime = timeSincePause;
    }
}

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

推荐阅读更多精彩内容

  • 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥ios动画全貌。在这里你可以看...
    每天刷两次牙阅读 12,713评论 6 30
  • 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥iOS动画全貌。在这里你可以看...
    F麦子阅读 10,526评论 5 13
  • 书写的很好,翻译的也棒!感谢译者,感谢感谢! iOS-Core-Animation-Advanced-Techni...
    钱嘘嘘阅读 6,811评论 0 6
  • Core Animation Core Animation,中文翻译为核心动画,它是一组非常强大的动画处理API,...
    45b645c5912e阅读 8,170评论 0 21
  • 在鸡呜鸟叫声中醒来 楼下一片喧哗 雨小了 继续一天的工作 行走在泥泞里 期盼下一个晴天
    大清晨的小太阳阅读 962评论 0 3