Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combine many UIViewPropertyAnimators

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?

like image 602
Tiago Almeida Avatar asked Nov 09 '17 17:11

Tiago Almeida


1 Answers

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.

like image 143
nathangitter Avatar answered Oct 09 '22 16:10

nathangitter