How to set animation curve when using UIView's keyframe animation :
animateKeyframesWithDuration:delay:options:animations:completion:
Whatever I do in the animation block seems to be linear (unless I use the UIViewKeyframeAnimationOptionCalculationModeCubic
option but this isn't what I want).
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:
Actually, you can use the same curve flags that you use for animateWithDuration:delay:options:animations:completion:. Just "binary or" them in with the other options in your call to animateKeyframesWithDuration:delay:options:animations:completion:
The documentation is confusing, but it does work.
The curve values you can use are:
The default animation curve you get is 0, or ease-in, ease-out.
Here's a nice and clean Swift solution I came up with:
Usage:
Swift 2.0 will not allow casting of
UIViewAnimationOptions
asUIViewKeyframeAnimationOptions
. It will also not allow|
ing them together.However, there is an initializer for
UIViewKeyframeAnimationOptions
that takes arawValue
. So, the following code works to putUIViewAnimationOptions
intoUIViewKeyframeAnimationOptions
:Then I can pass
keyframeAnimationOptions
toanimateKeyframesWithDuration
and everything works great.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.
The
frameStartTime
is your friend here... it will always be linear between keyframes (or paced / cubic / cubic paced, like defined in theUIViewKeyframeAnimationOptionCalculationMode
)To calculate correct timing values, you could use this as a reference: RBBEasingFunction
E.g.
EaseInOutQuad
like this (where t is the relative time within the animation):The answer by @eskimwier did not work for me when trying to set the curve to
linear
foranimateKeyframes(withDuration:delay:options:animations:completion:)
. This was in Swift 3, Xcode 8.2.1.My animation appeared to be defaulting to
easeInOut
(slow start up, faster in the middle, slow at the end). Did Apple change the default curve?Anyhow, I wanted a linear curve, and tried the approach suggested above to use
UIViewAnimationOptions
with keyframe animations:This had no effect. The only thing that worked for me was to do this before calling animateKeyframes: