我想检测当前页面,以便在最后一页显示时修改按钮的标题,并在每次用户从最后一页向最后一页向后滑动时重新修改。
现在这些是我的委托方法和我的ViewControllers数组:
lazy var VCArr: [UIViewController] = {
let firstVC = self.storyboard!.instantiateViewController(withIdentifier: "FirstTutorialPage")
let secondVC = self.storyboard!.instantiateViewController(withIdentifier: "SecondTutorialPage")
let thirdVC = self.storyboard!.instantiateViewController(withIdentifier: "ThirdTutorialPage")
let fourthVC = self.storyboard!.instantiateViewController(withIdentifier: "FourthTutorialPage")
return [firstVC, secondVC, thirdVC, fourthVC]
}()
委托方法
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
guard let viewControllerIndex = self.VCArr.index(of: viewController) else {
return nil
}
let previousIndex = viewControllerIndex - 1
guard previousIndex >= 0 else {
return nil
}
guard self.VCArr.count > previousIndex else {
return nil
}
return self.VCArr[previousIndex]
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
guard let viewControllerIndex = self.VCArr.index(of: viewController) else {
return nil
}
let nextIndex = viewControllerIndex + 1
guard nextIndex < self.VCArr.count else {
return nil
}
guard self.VCArr.count > nextIndex else {
return nil
}
return self.VCArr[nextIndex]
}
有人可以帮助我准确地检测显示的最后一页吗?我尝试了一些方法,但是他们每个人都向我展示了错误的索引。这些方法在显示倒数第二页时检测到最后一个索引。
感谢所有人。
答案 0 :(得分:4)
我的解决方案包括实现pageViewController(_:didFinishAnimating:previousViewControllers:transitionCompleted:)
的函数UIPageViewControllerDelegate
并执行以下操作:
if completed {
if let currentViewController = pageViewController.viewControllers?.first,
let index = pages.index(of: currentViewController) {
currentIndex = index
}
}
其中pages
是我的页面视图控制器的属性,我存储了要呈现的所有视图控制器,currentIndex
是一个属性,用于跟踪当前页面的索引(也称为视图控制器) )以便在我的代码的其他部分使用它。
答案 1 :(得分:0)
尝试在 pageViewController.viewControllers中获取第一个视图控制器
if let vc = pageViewController.viewControllers?[0] {
... vc is current controller...
}
如果要处理页面更改,请在委托方法中执行此操作:
func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
if let vc = pageViewController.viewControllers?[0] {
... vc is current controller...
}
}
答案 2 :(得分:0)
尝试此委托方法。
var currentIndex = 0
func pageViewController(_ pageViewController: UIPageViewController, willTransitionTo pendingViewControllers: [UIViewController]) {
guard let nextViewController = pendingViewControllers[0] as? ViewController else { return }
self.currentIndex = nextViewController.index
}
答案 3 :(得分:0)
像这样设置didFinishAnimating pageViewController DataSource函数。存储数据源的属性。在这种情况下,我将其命名为orderedViewControllers
。然后,在滑动时,当前索引将被打印到控制台。
func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
let pageContentViewController = pageViewController.viewControllers![0]
let index = orderedViewControllers.index(of: pageContentViewController)!
print(index)
}
答案 4 :(得分:-1)
您可以使用%
运算符
let nextIndex = (viewControllerIndex + 1) % VCArr.count
let previousIndex = (VCArr.count + viewControllerIndex - 1) % VCArr.count