iosswiftanimationdurationuiviewpropertyanimator

Finish some animations from UIViewPropertyAnimator earlier


I search for a way to add some animations to an UIViewPropertyAnimator which finishing earlier then others. UIViewPropertyAnimator have for example a method where you can add animations with a delay

animator.addAnimations(animation: (()-> Void), delayFactor: CGFloat)

so the animation starts at 50% of the duration at a delayFactor of 0.5.

I search for something like

animator.addAnimations(animation: (()->Void), realtiveDuration: CGFloat)

so the animation ends after 50% of the duration at a relativeDurationof 0.5.

After some research I found a solution by using

animator.addAnimations {
    UIView.animateKeyframes(withDuration: duration, delay: 0.0, animations: {
        UIView.addKeyframe(withRelativeStartTime: 0.0, relativeDuration: 0.3) {
            view.alpha = 0.0
        }
    })
}

to archive this behavior. Problem is, I want to use that in some kind of an automatism where I iterate through some elements and call a Method for each of the elements:

func animation(view: UIView) -> (() -> Void) {
    return {
        view.alpha = 0.0
    }
}

which works fine when using for example the method

animator.addAnimations(animation: element.animation(element.view), delayFactor: 0.5) but I cannot call this inside the

.addKeyframe(...){
    element.animation(element.view)
}

Maybe some of you guy's have a solution? I thinking about overriding UIViewPropertyAnimator for example to add the asked method animator.addAnimations(animation: (()->Void), realtiveDuration: CGFloat) or something, but leaving my comfortzone there.


Solution

  • You may call like this:

      UIView.addKeyframe(withRelativeStartTime: 0.5, relativeDuration: 0.5, animations: element.animation(element.view))