iOS Swift UIPageViewController不响应滑动

时间:2018-12-07 06:59:18

标签: ios swift uipageviewcontroller uiswipegesturerecognizer

我有这个非常奇怪的问题。我有一个项目中的页面视图控制器的简单实现。但是,页面浏览器控制器没有对滑动做出响应,或者有时它尝试滑动但又像挂起一样再次停止(不拖延),这对我来说真的很奇怪。我已经搜索了所有答案,但似乎都无法解决我的问题。请协助我。多谢。请参阅下面我用过的代码

  class PageViewController: UIPageViewController, UIPageViewControllerDataSource {


lazy var controllers: [UIViewController] = {
    let sb = UIStoryboard(name: "Onboarding", bundle: nil)

    let vc1 =  sb.instantiateViewController(withIdentifier: "RedVC")
    let vc2 = sb.instantiateViewController(withIdentifier: "PurpleVC")

    return [vc1, vc2]

}()

override func viewDidLoad() {
    super.viewDidLoad()

    self.dataSource = self

    if let firstVC = controllers.first {
        self.setViewControllers([firstVC], direction: .forward, animated: true, completion: nil)
    }

}


func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {

    guard let vcIndex = controllers.index(of: viewController) else { return nil }
    let previousIndex = vcIndex - 1
    guard previousIndex >= 0 else { return nil }
    guard controllers.count > previousIndex else { return nil }
    return controllers[previousIndex]
}

func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {

    guard let vcIndex = controllers.index(of: viewController) else { return nil }

    let nextIndex = vcIndex + 1

    guard controllers.count != nextIndex else { return nil }

    guard controllers.count > nextIndex else { return nil }

    return controllers[nextIndex]
  }

}

0 个答案:

没有答案