如何检查UIPageViewController VC何时更改为另一个?

时间:2018-03-08 13:35:37

标签: ios swift uipageviewcontroller

我尝试通过滑动检查我的UIPageViewController的VC何时更改为另一个。 什么函数处理这个,以及我如何获得新的页面索引?

我了解功能:

func pageViewController(_ pageViewController: UIPageViewController, willTransitionTo pendingViewControllers: [UIViewController]) {
  //code
}

非常感谢。

1 个答案:

答案 0 :(得分:1)

这一小段代码应该可以解决问题:

func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {

    guard completed else { return }

    guard let index = (pageViewController.viewControllers?.first as? ContentViewController)?.index else { return }

    guard let index = pageViewController.viewControllers?.first?.view.tag else { return }

    guard let vc = pageViewController.viewControllers?.first else { return }

    let index: Int

    switch vc {
    case is FirstViewController:
        index = 0
    case is SecondViewController:
        index = 1
    default:
        index = 2
    }
}

您在此处执行的操作是手动保存当前选定的索引。只要转换是动画的,这应该是有用的,我应该总是这样。