【问题标题】:Disable swipe in UIPageViewController在 UIPageViewController 中禁用滑动
【发布时间】:2015-07-24 02:08:21
【问题描述】:

我有一个 UIViewControllerUIPageViewController 变量。

我正在尝试禁用 UIPageViewController 在屏幕上某些位置的滑动。我通过将UIViewController 设置为UIGestureRecognizerDelegate 并将UIPageViewController 委托中的手势识别器设置为self(UIViewController)来做到这一点。我似乎无法找到手势识别器的位置或为UIPageViewController 加载。

我已经尝试了这三个地方,但在 viewDidLoad 方法中都是空的:

println("gesture\(self.pageViewController!.gestureRecognizers)")
println("gesture\(self.view.gestureRecognizers)")
println("gesture\(self.pageViewController!.view.gestureRecognizers)")

更新*******************:

所以我发现一些 UIGestureRecognizers 隐藏在 navigationController 中,但是将它们添加为委托并运行下面的代码什么也没做。是不是因为我有错误的 UIGestureRecognizers。那里也有一个 UIPanGestureRecognizer。为什么我的页面控制器或页面控制器的视图中没有任何识别器?

func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldReceiveTouch touch: UITouch) -> Bool {
    println("Should recieve touch")
    return false
}

我得到“应该接收触摸”以打印到日志,但我返回 false 并且没有禁用滑动。我做错了什么?

【问题讨论】:

    标签: ios swift uiviewcontroller uipageviewcontroller


    【解决方案1】:

    过去我曾尝试用 UIScrollView 做类似的事情。 UIScrollView 有一个名为 panGesture 的成员。但是您可以像这样在 pageViewControllerGesture 上循环(在标准的基于页面的应用程序上测试):

     override func viewDidLoad() {
        super.viewDidLoad()
    
        // other set up
    
        // loop over your pageViewController gestures
        for gesture in self.pageViewController!.gestureRecognizers
        {
            // get the good one, i discover there are 2
            if(gesture is UIPanGestureRecognizer)
            {
                // replace delegate by yours (Do not forget to implement the gesture protocol)
                (gesture as! UIPanGestureRecognizer).delegate = self
            }
        }
    
    }
    
    func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool
    {
        // add custom logic
        var ShouldISwipe = rand()%2 == 0
        println("Should I Swipe : \(ShouldISwipe)")
        return ShouldISwipe
    }
    

    我发现该链接也可以提供帮助(应该转换为 swift): http://www.lukaszielinski.de/blog/posts/2014/03/26/restrict-panning-of-uipageviewcontroller-to-certain-area/

    希望能有所帮助。

    【讨论】:

    • 问题是 self.pageViewController!.gestureRecognizers 返回一个空数组
    • 如果您有一个基于滚动的 UIPageViewController,链接中的解决方案和您在此处展示的信息是完美的解决方案。
    • 不,这不起作用,覆盖gestureRecognizer的委托会崩溃:*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'UIScrollView's built-in pan gesture recognizer must have its scroll view as its delegate.'
    【解决方案2】:

    您可以通过以下方式获取 UIPageViewController 手势识别器:

    UIPageViewController.view.subviews.first
    

    在我的测试中我得到:

        - [0] : <UIScrollViewDelayedTouchesBeganGestureRecognizer: 0x17e57f20; state = Possible; delaysTouchesBegan = YES; view = <_UIQueuingScrollView 0x1842b800>; target= <(action=delayed:, target=<_UIQueuingScrollView 0x1842b800>)>>
    - [1] : <UIScrollViewPanGestureRecognizer: 0x17e58470; state = Possible; delaysTouchesBegan = YES; delaysTouchesEnded = NO; view = <_UIQueuingScrollView 0x1842b800>; target= <(action=handlePan:, target=<_UIQueuingScrollView 0x1842b800>)>; must-fail = {
        <UIScrollViewPagingSwipeGestureRecognizer: 0x17d50110; state = Possible; view = <_UIQueuingScrollView 0x1842b800>; target= <(action=_handleSwipe:, target=<_UIQueuingScrollView 0x1842b800>)>>
    }>
    - [2] : <UIScrollViewPagingSwipeGestureRecognizer: 0x17d50110; state = Possible; view = <_UIQueuingScrollView 0x1842b800>; target= <(action=_handleSwipe:, target=<_UIQueuingScrollView 0x1842b800>)>; must-fail-for = {
        <UIScrollViewPanGestureRecognizer: 0x17e58470; state = Possible; delaysTouchesBegan = YES; delaysTouchesEnded = NO; view = <_UIQueuingScrollView 0x1842b800>; target= <(action=handlePan:, target=<_UIQueuingScrollView 0x1842b800>)>>
    }>
    

    【讨论】:

      【解决方案3】:

      您可以将此扩展程序用于 swift :

      extension UIPageViewController {
          var isPagingEnabled: Bool {
              get {
                  var isEnabled: Bool = true
                  for view in view.subviews {
                      if let subView = view as? UIScrollView {
                          isEnabled = subView.isScrollEnabled
                      }
                  }
                  return isEnabled
              }
              set {
                  for view in view.subviews {
                      if let subView = view as? UIScrollView {
                          subView.isScrollEnabled = newValue
                      }
                  }
              }
          }
      }
      

      并称之为:

      pageCtrl.isPagingEnabled = false
      

      【讨论】:

        【解决方案4】:

        我在 pageViewController 中有一个类似 tableview 的案例,当在单元格上滑动以显示删除选项时。但是 pageViewController 手势只是被触发了。

        所以为了保持 pagingView 滑动,同时允许您的内容控件使用它们的功能(滑动删除等),只需在 UIPageViewController 中关闭 canCancelContentTouches。

        在你的 UIPageViewController 的 viewDidLoad 中

        if let myView = view?.subviews.first as? UIScrollView {
            myView.canCancelContentTouches = false
        }
        

        【讨论】:

          猜你喜欢
          • 2016-12-07
          • 1970-01-01
          • 2018-11-01
          • 1970-01-01
          • 2014-04-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多