从第一页或0索引

时间:2018-01-08 09:50:50

标签: ios swipe uipageviewcontroller pop

我知道滑动默认由UIPageViewController管理,并且在此过程中会调用回调方法,如:willTransitionTo,didFinishAnimating等。

当我们从第一页或第零个索引向后滑动时,是否有可能解除UIPageViewController?

我检查过,发现没有为此操作调用任何回调方法。 任何帮助或建议表示赞赏。

3 个答案:

答案 0 :(得分:1)

我能够达到预期的效果,

1)将以下属性添加到UIPageViewController子类中:

var scrollView: UIScrollView?
var swipeBackPanGestureRecognizer: UIPanGestureRecognizer?

2)现在,将以下代码添加到UIPageViewController子类的viewDidLoad方法中:

scrollView = view.subviews.filter{ $0 is UIScrollView }.first as? UIScrollView

if let scrollView = scrollView,
   let popGestureRecognizer = self.navigationController?.interactivePopGestureRecognizer,
   let targets = popGestureRecognizer.value(forKey: "targets") as? NSMutableArray {
    let panGestureRecognizer = UIPanGestureRecognizer()
    panGestureRecognizer.setValue(targets, forKey: "targets")
    panGestureRecognizer.delegate = self
    scrollView.addGestureRecognizer(panGestureRecognizer)
    swipeBackPanGestureRecognizer = panGestureRecognizer
}

基本上,您要在scrollView中向构建中添加一个新的UIPanGestureRecognizer,该UIPanGestureRecognizer将从内置的InteractivePopGestureRecognizer中接管目标动作,并负责向后滑动(我在这里找到此技巧:https://stackoverflow.com/a/57487641/10060753)。

3)最后,您需要实现这两种协议方法。确保将以下代码添加到UIPageViewController子类的最后一个括号的下方,并用适当的子类名称替换“ YourPageViewController”:

extension YourPageViewController: UIGestureRecognizerDelegate {
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        guard let panRecognizer = gestureRecognizer as? UIPanGestureRecognizer,
            panRecognizer == swipeBackPanGestureRecognizer else {
            return true
        }

        guard let currentViewController = self.viewControllers?.first,
            pageViewController(self, viewControllerBefore: currentViewController) == nil else {
            return false
        }

        guard let gestureView = panRecognizer.view else {
            return true
        }

        let velocity = (panRecognizer.velocity(in: gestureView)).x
        return velocity > 0
    }

    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldBeRequiredToFailBy otherGestureRecognizer: UIGestureRecognizer) -> Bool {
        if gestureRecognizer == swipeBackPanGestureRecognizer && otherGestureRecognizer == scrollView?.panGestureRecognizer {
            return true
        } else {
            return false
        }
    }
}

gestureRecognizerShouldBegin(_ :)的实现可确保仅当我们在第一页上(a)第一页(UIPageViewControllerDataSource viewControllerBefore == nil)和b)从左向右滑动时(速度> 0)。

gestureRecognizer(_:shouldBeRequiredToFailBy :)的实现可确保仅当我们自己的向后滑动手势识别器失败时才触发内置的UIPageViewController panGestureRecognizer。

答案 1 :(得分:0)

按设计UIPageViewController不知道当前的视图控制器索引。每次调用UIViewController时,您都可以创建pageViewController(_:viewControllerAfter:)的新实例,并且您将有无限数量的页面转发。

我怀疑您通过它的数据源或setViewControllers(_:direction:animated:completion:)向UIPageViewController提供某种数据源。因此,您有责任跟踪当前显示的UIViewController的索引。

您可以通过访问previousViewControllers委托中UIPageViewController的第一个元素来查看正在显示的视图控制器:

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

答案 2 :(得分:0)

如果您的UIPageViewController在容器视图中,则需要传递UINavigationController或在容器的视图控制器中处理它:

override func viewDidLoad() {
    super.viewDidLoad()
    self.delegate = self
    self.dataSource = self
    for view in view.subviews
    {
        if let scrollView = view as? UIScrollView
        {
            scrollView.panGestureRecognizer.require(toFail: self.parentNavController!.interactivePopGestureRecognizer!);
        }
    }
    self.parentNavController!.interactivePopGestureRecognizer!.delegate = self
}

func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
    return self.parentNavController?.topViewController != self.parent || currentIndex == 0
}

在容器视图控制器中:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    switch (segue.identifier) {
        case "pageview_embed":
        let vc = segue.destination as! MyPageViewController
        vc.parentNavController = self.navigationController
        ...
        break
        ...
    }
    ...
}