I'm trying to perform a spring animation on a view which is released by the user's pan gesture and may have a non-zero velocity. I'm basically trying to recreate the animation of this WWDC video where they use UISpringTimingParameters(dampingRatio:initialVelocity:)
. However, the documentation seems to contradict itself:
velocity
The initial velocity and direction of the animation, specified as a unit vector.
[...]
For example, if the total animation distance is 200 points and the view’s initial velocity is 100 points per second, specify a vector with a magnitude of
0.5
.
If 0.5
is an example value, then apparently it doesn't need to be a unit vector after all. And it's not possible to encode a velocity in a unit vector in the first place.
Not being able to rely on the documentation, I tried plugging in several different values, but nothing lead to even remotely satisfactory results.
How do I use this API?
Good question.
TL;DR: If you are trying to animate something to a position in 2D, you need to animated each coordinate separately, each with the respective x / y velocity.
If you combine them by taking the scalar projection of the velocity onto your offset, you get a weird artifact where assuming the context of flicking a view around the screen, where the target is the center of the screen, and you are flicking the view up the right side, since the animation is to return to center, and since the combined velocity is going away from the center, the animation can only assume that the subject is moving in a straight line away from the center, and it will jag out to the right, before animating back to the center.
https://github.com/chrisco314/SpringAnimationTest