UIPageViewController: get the currentPage

后端 未结 12 561
别那么骄傲
别那么骄傲 2021-02-03 22:22

I have been struggling with this issue for the last few days and after all this juggling I have figured out that all I need is the current Index from the datasource method to up

12条回答
  •  旧时难觅i
    2021-02-03 22:45

    There is no nice way to do this but I think the most reliable way is to observe UIPageViewControllerDelegate calls:

    var index: Int
    var proposedViewController: UIViewController?
    
    // MARK: UIPageViewControllerDelegate
    
    func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
        guard let proposedViewController = proposedViewController,
            let index = myViewControllers?.index(of: proposedViewController) else {
                return
        }
        self.index = index
    }
    
    func pageViewController(_ pageViewController: UIPageViewController, willTransitionTo pendingViewControllers: [UIViewController]) {
        guard pendingViewControllers.count == 1,
            let proposedViewController = pendingViewControllers.first else {
            return
        }
        self.proposedViewController = proposedViewController
    }
    

提交回复
热议问题