iOS Swift: UIPageViewController - Turning page programmatically

后端 未结 6 825
我寻月下人不归
我寻月下人不归 2020-12-13 19:35

I have a UIPageViewController, which works fine when we swipe left or right to turn pages.

class ViewController: UIViewController, UIPageViewControllerDataSou         


        
6条回答
  •  情歌与酒
    2020-12-13 19:55

    Here is a swift implementation of this:

    private func slideToPage(index: Int, completion: (() -> Void)?) {
        let count = //Function to get number of viewControllers
        if index < count {
            if index > currentPageIndex {
                if let vc = viewControllerAtIndex(index) {
                    self.pageViewController.setViewControllers([vc], direction: UIPageViewControllerNavigationDirection.Forward, animated: true, completion: { (complete) -> Void in
                        self.currentPageIndex = index
                        completion?()
                    })
                }
            } else if index < currentPageIndex {
                if let vc = viewControllerAtIndex(index) {
                    self.pageViewController.setViewControllers([vc], direction: UIPageViewControllerNavigationDirection.Reverse, animated: true, completion: { (complete) -> Void in
                        self.currentPageIndex = index
                        completion?()
                    })
                }
            }
        }
    }
    

    viewControllerAtIndex(index: Int) -> UIViewController? is my own function to get the correct view controller to swipe to.

提交回复
热议问题