Randy
Randy

Reputation: 4525

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.

Upvotes: 2

Views: 1486

Answers (1)

KrishnaCA
KrishnaCA

Reputation: 5695

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 mentions in the comment and according to official documentation, Method 2 is the preferred method as it is a built in API which gives accurate time for animation to finish if animations proceeds without any userinteraction.

Upvotes: 1

Related Questions