NEW: Master Swift design patterns with my latest book! >>

How to make UIViewPropertyAnimator scrub with a custom curve: scrubsLinearly

Written by Paul Hudson    @twostraws

UIViewPropertyAnimator is an incredibly easy way to build custom animations on iOS, making it trivial to support scrubbable, reversible animations. However, by default scrubbing behaves differently from your regular animation: scrubbing always happens linearly, even if your animation was performed using a curve such as ease-in-ease-out.

This is actually a feature rather than a bug, and it’s incredibly intelligent – iOS automatically maps your curve onto a linear animation at the same point, and does the same in reverse, because it can be confusing for users if they try to scrub an animation and it doesn’t follow their finger.

On the flip side, of you’re scrubbing an animation programmatically this behavior isn’t always desirable. Fortunately, iOS 11 introduced a scrubsLinearly property for UIViewPropertyAnimator: set this to false to make scrubbing retain your custom animation curve.

The default setting is true, which makes UIViewPropertyAnimator continue working like it did on iOS 10. To change it, just add this code:

animator.scrubsLinearly = false

Available from iOS 11.0

Did this solution work for you? Please pass it on!

Other people are reading…

About the Swift Knowledge Base

This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions.

Want to build macOS apps?

Hacking with macOS delivers 18 awesome projects that teach you macOS development in no time – take control of your desktop today!

Click here to visit the Hacking with Swift store >>