在ios中使用关键帧动画时如何设置动画曲线?

问题描述:

如何在使用UIView的关键帧动画时设置动画曲线:

How to set animation curve when using UIView's keyframe animation :

animateKeyframesWithDuration:delay:options:animations:completion:

无论我在动画块中做什么似乎都是线性的(除非我使用 UIViewKeyframeAnimationOptionCalculationModeCubic 选项,但这不是我想要的。)

Whatever I do in the animation block seems to be linear (unless I use the UIViewKeyframeAnimationOptionCalculationModeCubic option but this isn't what I want).

我希望在动画上有一个缓出曲线,如 UIViewAnimationOptionCurveEaseOut 选项:

I'd like to have an ease out curve on the animation like the UIViewAnimationOptionCurveEaseOut option when using regular animation :

animateWithDuration:delay:options:animations:completion:


如果您使用的是关键帧,则必须自己定义曲线..如果添加线性关键帧,则会有线性动画。如果你添加非线性关键帧,你将有一个非线性动画。

If you are using keyframes, you have to define the curve on your own.. if you add linear keyframes, you have a linear animation. If you add non-linear keyframes, you will have a non-linear animation.

frameStartTime 是你的朋友这里......它们在关键帧之间总是线性的(或节奏/立方/立方节奏,如 UIViewKeyframeAnimationOptionCalculationMode 中定义的那样)

The frameStartTime is your friend here... it will always be linear between keyframes (or paced / cubic / cubic paced, like defined in the UIViewKeyframeAnimationOptionCalculationMode)

UIViewKeyframeAnimationOptionCalculationModeLinear     = 0 << 9,
UIViewKeyframeAnimationOptionCalculationModeDiscrete   = 1 << 9,
UIViewKeyframeAnimationOptionCalculationModePaced      = 2 << 9,
UIViewKeyframeAnimationOptionCalculationModeCubic      = 3 << 9,
UIViewKeyframeAnimationOptionCalculationModeCubicPaced = 4 << 9

要计算正确的时间值,您可以将其作为参考:
RBBEasingFunction

To calculate correct timing values, you could use this as a reference: RBBEasingFunction

EG EaseInOutQuad 喜欢这样(其中t是动画中的相对时间):

E.g. EaseInOutQuad like this (where t is the relative time within the animation):

if (t < 0.5) {
    return 2 * t * t;
} else {
    return -1 + (4 - 2 * t) * t;
}