您的位置:首页 > 编程语言

CAMediaTiming ( 时间协议)详解及实例代码

2016-12-12 15:05 633 查看

CAMediaTiming ( 时间协议)详解

有一种通过CAAnimation实现的协议叫做CAMediaTiming,也就是CABasicAnimation和CAKeyframeAnimation的基类(指CAAnimation)。像duration,beginTime和repeatCount这些时间相关的属性都在这个类中。大体而言,协议中定义了8个属性,这些属性通过一些方式结合在一起,准确的控制着时间。文档中每个属性只有几句话,所以很有可能在看这篇文章之前你都已经读过了,但是我觉得使用可视化的图形能更好的解释时间。

可视化的CAMediaTiming

为了显示相关属性的不同时间,无论是他们自己还是混合状态,我都会动态的将橙色变为蓝色。下面的块状显示了从开始到结束的动画过程,时间线上每一个标志代表一秒钟。你可以看到时间线上的任意一点,当前颜色即表示动画中的当前时间。比如,duration像下面一样可视。

我们都知道,CALayer和CAAnimation都实现了CAMediaTiming 协议,因此在Core Animation中,理解CAMediaTiming协议中的属性是非常必要的,但是苹果的文档中对于各个属性描述太简单,对初学者容易理解,这篇文章主要帮助理解CAMediaTiming协议中各个属性的含义。

CAMediaTiming Protocol提供了8个属性,下面将分别讲解。

CAMediaTiming / 时间协议

  1. repeatCount,动画的重复次数,可以设置为小数。设置为HUGE_VALF,表示无限重复。
  2. repeatDuration,动画总时长,如果大于单次时长,则重复;如果小于单次时长,则截断。
  3. duration,单次动画时长。
  4. speed,图层或动画模型相对于父图层CALayer的时间流逝速度。
  5. fillMode,有效期结束后,动画对象的呈现效果是冻结还是移除。
  6. beginTime,相对于父对象的开始时间。注意,以系统的绝对时间为准。例如:
/**
当前时间2秒以后开始动画
*/
keyFrameAnim.beginTime = CACurrentMediaTime() + 2;
/**
截止到当前时间,动画已经执行了2秒,
注意,如果执行的时间大于动画时长,则表示动画已经执行过。
*/
keyFrameAnim.beginTime = CACurrentMediaTime() - 2;

7.timeOffset,时间轴偏移量。将时间轴移动至偏移位置,再执行整个动画时长。假设动画时长3秒,偏移量为8,则开始位置为8 % 3 = 2,再执行3秒,即在整个时长的1/ 3处结束。

8.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;
}
}

感谢阅读,希望能帮助到大家,谢谢大对本站的支持!

您可能感兴趣的文章:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息