Combine many UIViewPropertyAnimators
Asked Answered
B

1

9

I was messing with the documentation of UIViewPropertyAnimator and I was hoping to find a way to combine two UIViewPropertyAnimator.

Something like this:

let firstAnimator = UIViewPropertyAnimator(
   duration: 2, 
   dampingRatio: 0.4, 
   animations: animation1
)

let secondAnimator = UIViewPropertyAnimator(
    duration: 2, 
    dampingRatio: 0.4,
    animations: animation2
)

firstAnimator.addAnimator(secondAnimator, withDelay: 1)
firstAnimator.startAnimation()

I am missing a UIViewPropertyAnimatorGroup or something like this does this even exist?

Borlow answered 9/11, 2017 at 17:36 Comment(0)
U
4

The current recommended method is to use an array of UIViewPropertyAnimator's.

This approach is outlined in detail in a WWDC 2017 presentation titled "Advanced Animations with UIKit". https://developer.apple.com/videos/play/wwdc2017/230/

The essential bit is to keep all your animators together, and whenever a gesture-based event occurs you can apply an update to all animators at once.

var runningAnimators = [UIViewPropertyAnimator]()

// later in the code...

runningAnimators.forEach { $0.pauseAnimation() } // pause all animations

I have an open-source repo on GitHub that gives an example of using multiple property animators in this way: https://github.com/nathangitter/interactive-animations


For your specific question, why do you need multiple property animators? Generally you only need multiple animators when you want a single animation with multiple timing curves, but it looks like your timing parameters are identical.

Unfeigned answered 9/11, 2017 at 17:56 Comment(4)
I want to abstract the composition of animations. There are two scenarios where this is useful: 1) after one animator completes, call another, and after that one completes, call another (and abstract this to work at the animators layer) 2) if you want to do one animator, start one after some time and when both wrap up call a completion handler (I know you can add an animation with a delay, but the animation still completes in the same time as the animator, and it makes sense)Borlow
You might want to write your own wrapper around Core Animation instead. Is there a specific kind of animation you are trying to achieve? There are some good libraries that can abstract certain kinds of animations. UIViewPropertyAnimator seems to be best suited for animations that need to be gesture-driven and interruptible.Unfeigned
@TiagoAlmeida Did this solution end up working for you?Unfeigned
Well, if you want an interruptible transition alas you have to have a single UIViewPropertyAnimator :-[Lorentz

© 2022 - 2024 — McMap. All rights reserved.