我想知道如何获得此方法的动画持续时间:
setViewControllers(_ viewControllers: [UIViewController]?,
direction: UIPageViewControllerNavigationDirection,
animated: Bool,
completion: ((Bool) -> Void)? = nil)
animated
参数设置为true
。
答案 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在评论中提及并且根据官方documentation,Method 2
是首选方法,因为它是一个内置API,可以在动画进行时为动画提供准确的时间没有任何用户交互。