我有一个适用于iPad / iPhone的应用程序,它完全适用于所有版本,适用于所有版本,但iPhone 4S,5和5s.
我有一个UIPageViewController与WebViews作为单元格.
我有另一个简单的UICollectionView用作页面选择器:它的功能是让用户跳转到选中的第X页.
我的代码非常简单:我将触摸的单元格的索引传递给UIPageViewController以应用我的“slideToPage(index)”函数.
对于iPhone 4S / 5 / 5S,当动画完成后,它会立即返回到旧页面,因此用户无法跳转页面,他只能一次翻页:
这是我的代码:
func slideToPage(index: Int, completion: (() -> Void)?) {
let currentViewController = pageViewController?.viewControllers![0] as! WebViewViewController
let count = dataSource.controllers.count
let currentPageIndex = dataSource.controllers.indexOf(currentViewController)!
guard index < count else { return }
// Moving forward
if index > currentPageIndex {
if let vc: WebViewViewController = dataSource.controllers[index] {
self.pageViewController!.setViewControllers([vc], direction: UIPageViewControllerNavigationDirection.Forward, animated: true, completion: {
void in
vc.loadContent()
})
}
}
// Moving backward
else if index < currentPageIndex {
if let vc: WebViewViewController = dataSource.controllers[index] {
self.pageViewController!.setViewControllers([vc], direction: UIPageViewControllerNavigationDirection.Reverse, animated: true, completion: {
void in
vc.loadContent()
})
}
}
}
编辑:
问题出在动画中.如果在setViewControllers()函数内设置动画:false,则视图会转到正确的视图.
如果为true,则在动画结束时,网页浏览将转到上一页.
最佳答案 经过一番尝试,目前我解决了在self.pageViewController!.setViewControllers()中设置false的动画参数.
似乎是一个iOS错误.
所以,我添加了一个isAnimated:bool var,仅用于iPad设备的动画;这是我的更新:
func slideToPage(index: Int, completion: (() -> Void)?) {
if index >= dataSource.controllers.count || index < 0 {
return
}
let currentViewController = pageViewController?.viewControllers![0] as! WebViewViewController
let currentPageIndex = dataSource.controllers.indexOf(currentViewController)!
let isAnimated = (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.Pad)
// Moving forward
if index > currentPageIndex {
if let vc: WebViewViewController = dataSource.controllers[index] {
self.pageViewController!.setViewControllers([vc], direction: UIPageViewControllerNavigationDirection.Forward, animated: isAnimated, completion: { complete in
vc.loadContent()
})
}
}
// Moving backward
else if index < currentPageIndex {
if let vc: WebViewViewController = dataSource.controllers[index] {
self.pageViewController!.setViewControllers([vc], direction: UIPageViewControllerNavigationDirection.Reverse, animated: isAnimated, completion: { complete in
vc.loadContent()
})
}
}
}