Reliable way to track Page Index in a UIPageViewController - Swift

倖福魔咒の 提交于 2019-12-11 05:42:34

问题


I have a UIPageViewController (custom one) inside a Container located in a regular UIViewController. i need to be able to call an event with each Page Change but ONLY if it really did change and not only half way or anything of that sort.

using:

func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController?

is unreliable and not called each time for some reason.

if your answer contains anything about willTransitionToViewControllers or didFinishAnimating please elaborate and not just mention them, since i already know they exist but dont understand the proper way to use them.

Thank you


回答1:


Use didFinishAnimating it has a completed and finished property so you know the page has actually changed. From the pageViewController you can get the currently displayed page, then get the position of this VC in your model.

First make sure your ViewController adopts UIPageViewControllerDelegate

Set the delegate (e.g. in viewDidLoad)

pageViewController.delegate = self

Then implement the following function:

    func pageViewController(pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
        if (completed && finished) {
            if let currentVC = pageViewController.viewControllers?.last {
                let index = myViewControllers.indexOf(currentVC)
                //do something with index
            }
        }
    }


来源:https://stackoverflow.com/questions/38610670/reliable-way-to-track-page-index-in-a-uipageviewcontroller-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!