我有一个可在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()函数中设置animation:false,页面视图就会转到正确的视图。 如果设置为true,在动画结束时,页面视图会转到上一页。