Tom Hamming Tom Hamming - 4 months ago 14
iOS Question

How do duration and UISpringTimingParameters interact in UIViewPropertyAnimator?

iOS 10 has a couple of new classes for animating. Notable ones include

and
UISpringTimingParameters
. The property animator can be created with a duration and timing parameters - the timing parameters can be an instance of the spring parameters.

One common complaint about spring animation APIs in iOS in the past is that they require a duration - the duration of a real spring animation should be determined by the spring properties like damping ratio and initial velocity.
UISpringTimingParameters
can be created with those kinds of parameters.

My question: Does
UIViewPropertyAnimator
still use the duration you give it when its timing curve is a spring? Or will it discard the duration and use the spring parameters to calculate it? That would be a weird API decision on Apple's part, but it also seems weird to come so close to giving designers what they want (real springs) and yet not quite get there (force specifying a duration).

Answer

UIViewPropertyAnimator is like Object-Oriented version of UIView Animation. Note: UISpringTimingParameters include two kinds of init methods: init(dampingRatio: CGFloat, initialVelocity: CGVector) like UIView Spring Animation API, which is a simplified version of spring animation; init(mass: CGFloat, stiffness: CGFloat, damping: CGFloat, initialVelocity: CGVector) like Core Spring Animation API which is complete spring animation.

Answer: with a spring timing of init(dampingRatio: CGFloat, initialVelocity: CGVector), UIViewPropertyAnimator instance's animation duration will be duration you set; with a spring timing of init(mass: CGFloat, stiffness: CGFloat, damping: CGFloat, initialVelocity: CGVector), UIViewPropertyAnimator instance's animation duration will be one this spring timing calculate. It's easy to verify it. You could use my demo: ControlPanelAnimation.

UIView Spring Animation API is a simplified version of Core Spring Animation API, just as the article you cited say, UIView Spring Animation API is not good at real oscillation, but it's easy to control: most time you need an animation you sure its duration. Core Spring Animation API: CASpringAnimation, it has real spring behavior but it's not easy to control its duration, its animation duration is up to its four physical parameters.