UIViewPropertyAnimator with short animation segments in the beginning

353 views Asked by At

Imagine UIViewPropertyAnimator is setup as follows:

let animator = UIViewPropertyAnimator(duration: 2, curve: .easeInOut) { 
   myView.center = newCenterPoint
}

For this, I can add another animation that starts with a delay, ends with the main:

animator.addAnimation ({
   myView.alpha = 0.5
}, withDelayFactor: 0.5)

So above starts at 1 second and continues for 1 second to finish with the main.

Question

Is there a way to add an animation to UIViewPropertyAnimator that starts at the beginning, but continues for a fraction of the time of the main animator? Starts at 0 and continues only for 1 second?

1

There are 1 answers

2
matt On

Why not just use another animator? It's fine for two animators to act on the same view simultaneously as long as their animations don't conflict:

    let animator = UIViewPropertyAnimator(duration: 2, curve: .easeInOut) {
        self.myView.center.y += 100
    }
    let animator2 = UIViewPropertyAnimator(duration: 1, curve: .easeInOut) {
        self.myView.backgroundColor = .red
    }
    animator.startAnimation(); animator2.startAnimation()

The animation moves the view downward for two seconds, while turning the view red during the first one second.

(Having said all that, in real life I'd probably use a CAAnimationGroup for this, rather than a property animator.)