关键帧animation关键时刻

我刚刚创build了一个这样的关键帧animation:

[UIView animateKeyframesWithDuration:10 delay:0 options:0 animations:^{ [UIView addKeyframeWithRelativeStartTime:0 relativeDuration:.1 animations:^{ view.alpha = 0; }]; } completion:nil]; 

这是一个CAKeyframeAnimation被创build:

 (lldb) po [self.layer animationForKey:@"opacity"] <CAKeyframeAnimation:0x10a6364b0; keyTimes = ( 0, "0.1", 1 ); values = ( 1, 0, 0 ); calculationMode = linear; delegate = <UIViewKeyframeAnimationState: 0x10a6358d0>; fillMode = both; timingFunction = easeInEaseOut; duration = 10; keyPath = opacity> 

题:

整个animation需要10秒,不透明度animation10 * 0.1 = 1秒,对吧? 当我看animation,变化是animation方式超过1秒。

为什么?

原因是animation的定时function不是线性的。

“时间和空间是相对的概念。” – 相对论

层和animation使用分层时序系统,每个对象都有自己的本地时间,这取决于它的父母和它自己的时间参数。

例如,animation具有定义其调步的定时function。 UIKit创build的animation的默认定时function是易入式定时function,该function定义了一个缓慢开始的时间,在整个持续时间中加速,然后在完成之前再次减速。 它使平滑的animation不会突然启动或停止。 但是,当你需要精确的时机时,它也会随着你的关键帧animation的关键时刻而变得烦人。

您可以通过将UIViewAnimationOptionCurveLinear选项传递给animateKeyframesWithDuration:delay:options:animations:completion:方法来禁用易入缓出时间。 我不确定是否需要将UIViewAnimationOptions的值UIViewKeyframeAnimationOptionsUIViewKeyframeAnimationOptions ,文档中没有提供任何相关信息。 也许更好的(但更详细的)方法是将关键帧animation块embedded到如下的标准animation块中:

 [UIView animateWithDuration:10 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{ [UIView animateKeyframesWithDuration:0 /*inherited*/ delay:0 options:0 animations:^{ [UIView addKeyframeWithRelativeStartTime:0 relativeDuration:.1 animations:^{ view.alpha = 0; }]; } completion:nil]; } completion:nil];