UIPageViewController的setViewControllers方法的动画持续时间是多少

时间:2016-11-10 15:59:41

标签: ios swift animation

我想知道如何获得此方法的动画持续时间:

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

animated参数设置为true

1 个答案:

答案 0 :(得分:1)

我相信以下代码应该完成这项工作。如果有其他方式,请告诉我

方法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)
})

方法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)
   }
})

修改

正如rickster在评论中提及并且根据官方documentationMethod 2是首选方法,因为它是一个内置API,可以在动画进行时为动画提供准确的时间没有任何用户交互。