UIPageControl 未正确更新为 UIPageViewController 中当前页面的索引
UIPageControl not correctly updating to index of current page in UIPageViewController
我有一个 UIPageViewController,它以编程方式内置了一个 PageControl。我有一个数组,用于跟踪可以分页的视图控制器,我正在尝试让 PageControl 显示与当前相关的点查看控制器页面。
我有以下用于向后和向前分页的代码:
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
guard let vcIndex = viewControllerList.index(of: viewController) else {
return nil
}
let previousIndex = vcIndex - 1
guard previousIndex >= 0 else {return nil}
guard viewControllerList.count > previousIndex else {return nil}
self.pageControl.currentPage = previousIndex
return viewControllerList[previousIndex]
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
guard let vcIndex = viewControllerList.index(of: viewController) else {return nil}
let nextIndex = vcIndex + 1
guard viewControllerList.count != nextIndex else {return nil}
guard viewControllerList.count > nextIndex else {return nil}
self.pageControl.currentPage = nextIndex
return viewControllerList[nextIndex]
}
当我将 PageControl 的当前页面设置为 nextIndex 或 previousIndex 时,我相信它应该为正在显示的当前视图控制器的页面显示正确的点,但是它经常错过中间索引(有三个点总数)。它通常只是在第 1 个和第 3 个点之间来回移动,并且不准确。
如果有更好的方法,我也会接受其他答案。
是因为你把pageControl的当前索引设置错了。您必须将其设置为:
func pageViewController(UIPageViewController, willTransitionTo: [UIViewController])
Called before a gesture-driven transition begins.
或
func pageViewController(UIPageViewController, didFinishAnimating: Bool, previousViewControllers: [UIViewController], transitionCompleted: Bool)
Called after a gesture-driven transition completes.
这两种方法都来自 UIPageViewControllerDelegate
我有一个 UIPageViewController,它以编程方式内置了一个 PageControl。我有一个数组,用于跟踪可以分页的视图控制器,我正在尝试让 PageControl 显示与当前相关的点查看控制器页面。
我有以下用于向后和向前分页的代码:
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
guard let vcIndex = viewControllerList.index(of: viewController) else {
return nil
}
let previousIndex = vcIndex - 1
guard previousIndex >= 0 else {return nil}
guard viewControllerList.count > previousIndex else {return nil}
self.pageControl.currentPage = previousIndex
return viewControllerList[previousIndex]
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
guard let vcIndex = viewControllerList.index(of: viewController) else {return nil}
let nextIndex = vcIndex + 1
guard viewControllerList.count != nextIndex else {return nil}
guard viewControllerList.count > nextIndex else {return nil}
self.pageControl.currentPage = nextIndex
return viewControllerList[nextIndex]
}
当我将 PageControl 的当前页面设置为 nextIndex 或 previousIndex 时,我相信它应该为正在显示的当前视图控制器的页面显示正确的点,但是它经常错过中间索引(有三个点总数)。它通常只是在第 1 个和第 3 个点之间来回移动,并且不准确。
如果有更好的方法,我也会接受其他答案。
是因为你把pageControl的当前索引设置错了。您必须将其设置为:
func pageViewController(UIPageViewController, willTransitionTo: [UIViewController])
Called before a gesture-driven transition begins.
或
func pageViewController(UIPageViewController, didFinishAnimating: Bool, previousViewControllers: [UIViewController], transitionCompleted: Bool)
Called after a gesture-driven transition completes.
这两种方法都来自 UIPageViewControllerDelegate