How can I calculate the duration of a UIViewAnimationCurveEaseOut animation with a known initial speed and distance?

I animate the presentation between two points using the UIViewAnimationCurveLinear, so I know the speed of this animation. In some cases, I want to add a UIViewAnimationCurveEaseOut to make the view slow to a stop. To make this effect unexcited, the relief animation should start at the same speed as the previous linear animation. Given the fixed distance at which I want this attenuation to occur, how can I calculate the duration needed to reach this known initial speed?

For example, let's say I animate my view from x = 0 to x = 100 in 10 seconds. Thus, the speed is 10 pixels per second. Now I want the view to slow down from x = 100 to x = 120 using the UIViewAnimationCurveEaseOut animation. How long is this animation to start at 10 pixels per second?

I understand that Core Animation CAMediaTimingFunction controls the movement of the animation using cubic Bezier curves, where the second and third control points determine the shape of the curve. I believe the UIViewAnimationCurve attenuation functions are also cubic Bezier curves. If I knew the default control points used by these functions, I could develop a formula for calculating the duration of a given speed and distance, but I could not find these default control points, somewhere documented.

+4
source share
2 answers

I may not be able to give you a complete answer, but I can point to the CAMediaTimingFunction -getControlPointAtIndex: values: method. This should allow you to create an EaseOut synchronization function and then test its breakpoints.

I will also point you to an article by Matt Gallagher about custom acceleration curves made using CAKeyframeAnimation, which may also be of use to you.

+1
source

You can use a constant (name it BEZIER_INTEGRAL_CONSTANT ), which approximates the Bezier curve integral for an animation that is 1 second, starting at a speed of 1 pixel per second. For linear attenuation, this constant is 0.5 . For UIViewAnimationCurveEaseOut it is approximately t23. Formula in terms of duration:

 duration = distance / (velocity * BEZIER_INTEGRAL_CONSTANT) 

For your example, when the distance is 20 pixels, and the initial velocity is 10 pixels per second, the length should be approximately: 20 / (10 * 0.7) = 2.9 seconds .

0
source

All Articles