UIPageViewController - how Can I jump to a particular page number?

前端 未结 5 889
耶瑟儿~
耶瑟儿~ 2020-12-15 20:57

I have spent almost 8 hours finding out how to jump to a particular page number in UIPageViewController... below is what my project looks like

I want to make an app

5条回答
  •  暖寄归人
    2020-12-15 21:46

    @milijan answer worked great. Here is the swift version of his answer:

    func jumptoPage(index : Int) {
    
        let vc = viewControllerAtIndex(index)
        let direction : UIPageViewControllerNavigationDirection!
    
        if currentIndex < index {
            direction = UIPageViewControllerNavigationDirection.Forward
        }
        else {
            direction = UIPageViewControllerNavigationDirection.Reverse
        }
    
        if (currentIndex < index) {
            for var i = 0; i <= index; i++ {
                if (i == index) {
                    self.newPageViewController.setViewControllers([vc], direction: direction, animated: true, completion: nil)
                }
                else {
                    self.newPageViewController.setViewControllers([viewControllerAtIndex(i)], direction: direction, animated: false, completion: nil)
                }
            }
        }
        else {
            for var i = currentIndex; i >= index; i = i - 1 {
                if i == index {
                    self.newPageViewController.setViewControllers([vc], direction: direction, animated: true, completion: nil)
                }
                else {
                    self.newPageViewController.setViewControllers([viewControllerAtIndex(i)], direction: direction, animated: false, completion: nil)
                }
            }
        }
     currentIndex = index
    }
    

提交回复
热议问题