UIViewControllerAnimatedTransitioning transtionsDuration and UIView.Animate

50 views Asked by At

I have a subclass of UIViewControllerAnimatingTransitioning which defines an animations in a method animateTransitioning(using:) with UIView.animate(withDuration: 1) and second one UIView.animate(withDuration: 2, delay: 0.5, options: .curveEaseIn). The UIViewControllerAnimatingTransitioning requires a second method which is transitionDuration(using:) which should return a duration.

How duration defined in transitionDuration(using:) impact the duration defined in UIView.animation(withDuration)?

1

There are 1 answers

0
Thisura Dodangoda On

According to the documentation provided by Apple for the transitionDuration(using:) method here, the duration you return is used to synchronize other potential animations and "actions".

UIKit uses the value to synchronize the actions of other objects that might be involved in the transition. For example, a navigation controller uses the value to synchronize changes to the navigation bar.

In your scenario where there are two different UIView.animate blocks, you should return the time it takes for the longest animation to complete. Below is the explanation on how to calculate this. Assume both animations start at t = 0, where t is time in seconds.

totalAnimationDuration = delay + duration
  1. First animation starts at, t = 0 and completes after 1 second.
  2. Second animation starts at, t = 0.5 and completes after 2 seconds.
let animOneTotalDuration = 0 + 1 // 1 second
let animTwoTotalDuration = 0.5 + 2 // 2.5 seconds

Since the animation that has the maximum total time is the second animation, you should return 2.5 at transitionDuration(using:).

It should also be noted that setting the animation duration correctly does not automatically complete the transition after the duration. You must still invoke the completeTransition(_:) of the UIViewControllerContextTransitioning object you receive at animateTransition(using:).

Hope I was able to give you some insight!