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

你可能感兴趣的:(CAMediaTiming / 时间协议)