Randy Randy - 29 days ago 6
Swift Question

What is the animation duration of UIPageViewController's setViewControllers method

I was wondering how one could get the animation duration of this method:

setViewControllers(_ viewControllers: [UIViewController]?,
direction: UIPageViewControllerNavigationDirection,
animated: Bool,
completion: ((Bool) -> Void)? = nil)


When the
animated
parameter is set to
true
.

Answer

I believe the following code should do the work. Please let me know if there is any other way

Method 1:

let initialTime:CFAbsoluteTime = CFAbsoluteTimeGetCurrent()

let pager:UIPageViewController = UIPageViewController()
pager.setViewControllers(nil, direction: UIPageViewControllerNavigationDirection.forward, animated: true, completion: {(success) -> Void in
   let animationTime: CFTimeInterval = CFAbsoluteTimeGetCurrent() - initialTime
   print(animationTime)
})

Method 2:

let pager:UIPageViewController = UIPageViewController()
   pager.setViewControllers(nil, direction: UIPageViewControllerNavigationDirection.forward, animated: true, completion: {(success) -> Void in
   if let transiton = pager.transitionCoordinator {
      print(transiton.transitionDuration)
   }
})

Edit:

As rickster says, Method 2 is the preferred method.