iOS Swift: UIPageViewController - 以编程方式翻页

iOS Swift: UIPageViewController - Turning page programmatically

我有一个 UIPageViewController,当我们向左或向右滑动翻页时,它工作正常。

class ViewController: UIViewController, UIPageViewControllerDataSource {
...
}

现在我打算在页面上也提供Previous/Next按钮,这样点击这些按钮就可以翻页了。

如何以编程方式触发滑动 left/right 行为或翻页?


备注

这是 Swift 语言的问题,而不是 Objective-C。

使用此功能并为您想要的动画设置过渡样式。

这是一个 swift 实现:

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? 是我自己的函数,用于获取要滑动到的正确视图控制器。

通常不需要 fiddle 页面索引等等。您可能已经在实施 dataSource: UIPageViewControllerDataSource,其中包含显示上一个或下一个 ViewController 所需的所有内容。

Swift 5 示例:

extension UIPageViewController {

    func goToNextPage() {
       guard let currentViewController = self.viewControllers?.first else { return }
       guard let nextViewController = dataSource?.pageViewController( self, viewControllerAfter: currentViewController ) else { return }
       setViewControllers([nextViewController], direction: .forward, animated: false, completion: nil)
    }

    func goToPreviousPage() {
       guard let currentViewController = self.viewControllers?.first else { return }
       guard let previousViewController = dataSource?.pageViewController( self, viewControllerBefore: currentViewController ) else { return }
       setViewControllers([previousViewController], direction: .reverse, animated: false, completion: nil)
    }

}

通过这种方式,您可以确保要转换到的页面正是 PageViewController's 内置手势所触发的页面。

Swift 3 版本的 SuitedSloth 答案(根据需要对 animated 参数进行了小幅调整默认情况下是动画的,但仍然在函数中使用参数)以防万一有人需要它:

extension UIPageViewController {

    func goToNextPage(animated: Bool = true) {
        guard let currentViewController = self.viewControllers?.first else { return }
        guard let nextViewController = dataSource?.pageViewController(self, viewControllerAfter: currentViewController) else { return }
        setViewControllers([nextViewController], direction: .forward, animated: animated, completion: nil)
    }

    func goToPreviousPage(animated: Bool = true) {
        guard let currentViewController = self.viewControllers?.first else { return }
        guard let previousViewController = dataSource?.pageViewController(self, viewControllerBefore: currentViewController) else { return }
        setViewControllers([previousViewController], direction: .reverse, animated: animated, completion: nil)
    }

}

如果有人想使用定义的协议以编程方式导航和更改视图控制器,请将其留在这里。

@objc protocol AppWalkThroughDelegate {
  @objc optional func goNextPage(forwardTo position: Int)
  @objc optional func goPreviousPage(fowardTo position: Int)
}

使用上面的协议并确认到 root UIPageViewController 来管理视图控制器之间的导航。

示例如下:

class AppWalkThroughViewController: UIPageViewController, UIPageViewControllerDataSource, AppWalkThroughDelegate {

    // Add list of view controllers you want to load

    var viewControllerList : [UIViewControllers] = {
       let firstViewController = FirstViewController()
       let secondViewController = SecondViewController() 
       // Assign root view controller as first responder
       secondViewController.delegate = self  

       let thirdViewController = ThirdViewController() 
       
    }

    override func viewDidLoad() {
       super.viewDidLoad()
    }
    
    // Navigate to next page 
    func goNextPage(fowardTo position: Int) {
       let viewController = self.viewControllerList[position]
       setViewControllers([viewController], direction: 
       UIPageViewControllerNavigationDirection.forward, animated: true, completion: nil)
    }

    }

完成所有这些后,需要使 UIPageViewController 移动到下一页或上一页的子视图控制器可以通过将特定数字传递给委托来使用 AppWalkThroughDelegate 方法 属性。

示例如下:按下按钮后调用委托方法

    class SecondViewController: UIViewController {
       
        var delegate: AppWalkThroughDelegate!
        
        override func viewDidLoad() {
           super.viewDidLoad()
        }

         @IBAction func goNextPage(_ sender: Any) {
            // Can be any number but not outside viewControllerList bounds 
            delegate.goNextPage!(fowardTo: 2)
         }
    }

这里也是 swift 4 代表的实施。

因为我还使用了 UIPageViewControllerDelegate,并且大多数 setViewController 解决方案都没有调用委托方法。

感谢 Andrew Duncan's 对代表的评论。

// Functions for clicking next and previous in the navbar, Updated for swift 4
@objc func toNextArticle(){
    guard let currentViewController = self.viewControllers?.first else { return }

    guard let nextViewController = dataSource?.pageViewController( self, viewControllerAfter: currentViewController ) else { return }

    // Has to be set like this, since else the delgates for the buttons won't work
    setViewControllers([nextViewController], direction: .forward, animated: true, completion: { completed in self.delegate?.pageViewController?(self, didFinishAnimating: true, previousViewControllers: [], transitionCompleted: completed) })
}

@objc func toPreviousArticle(){
    guard let currentViewController = self.viewControllers?.first else { return }

    guard let previousViewController = dataSource?.pageViewController( self, viewControllerBefore: currentViewController ) else { return }

    // Has to be set like this, since else the delgates for the buttons won't work
    setViewControllers([previousViewController], direction: .reverse, animated: true, completion:{ completed in self.delegate?.pageViewController?(self, didFinishAnimating: true, previousViewControllers: [], transitionCompleted: completed) })
}