【问题标题】:UIPageViewController disable scrollingUIPageViewController 禁用滚动
【发布时间】:2012-11-14 05:32:30
【问题描述】:

我正在使用带有 transitionStyle UIPageViewControllerTransitionStyleScroll 和 navigationOrientation UIPageViewControllerNavigationOrientationVertical 的 UIPageViewController

我在视图上还有一个UIPanGestureRecognizer,我想在平移手势处于活动状态时禁用页面滚动。

我正在尝试在手势开始时设置以下内容:

pageViewController.view.userInteractionEnabled = NO;

这似乎没有效果,或者它似乎偶尔起作用。

我发现的唯一其他方法(可行)是在平移手势运行时将 UIPageViewController 数据源设置为 nil,但是这会在重置数据源时导致巨大的延迟。

【问题讨论】:

    标签: ios6 uipageviewcontroller


    【解决方案1】:

    UIPageViewController 使用一些 UIScrollView 对象来处理滚动(至少对于 transitionStyle UIPageViewControllerTransitionStyleScroll)。您可以通过控制器的子视图pageViewController.view.subviews 进行迭代以获取它。现在,您可以轻松启用/禁用滚动:

    - (void)setScrollEnabled:(BOOL)enabled forPageViewController:(UIPageViewController*)pageViewController
    {
        for (UIView *view in pageViewController.view.subviews) {
            if ([view isKindOfClass:UIScrollView.class]) {
                UIScrollView *scrollView = (UIScrollView *)view;
                [scrollView setScrollEnabled:enabled];
                return;
            }
        }
    }
    

    【讨论】:

    • 我已经搜索了一段时间,这是我找到的关于禁用 UIPageViewController 滑动但保持点击移动的最佳答案。
    • 在 swift 中,如果你喜欢简洁的东西有点难以阅读:pageViewController.view.subviews.flatMap({ $0 as? UIScrollView}).forEach({$0.isScrollEnabled = enabled})
    【解决方案2】:

    对于那些使用 swift 而不是 Objective-c 的人,这里是 Squikend 的转置解决方案。

    func findScrollView(#enabled : Bool) {
        for view in self.view.subviews {
          if view is UIScrollView {
            let scrollView = view as UIScrollView
            scrollView.scrollEnabled = enabled;
          } else {
            println("UIScrollView does not exist on this View")
          }
    
        }
      }
    

    【讨论】:

      【解决方案3】:

      每个人都非常非常复杂。

      这是禁用或启用滚动所需的全部功能。

          func enableScroll(_ enable: Bool) {
              dataSource = enable ? self : nil
          }
      

      【讨论】:

        【解决方案4】:

        Swift 4.2 版本的答案

        func findScrollView(enabled: Bool) {
            for view in self.view.subviews {
                if view is UIScrollView {
                    let scrollView = view as! UIScrollView
                    scrollView.isScrollEnabled = enabled
                } else {
                    print("UIScrollView does not exist on this View")
                }
        
            }
        }
        

        然后 yourpagecontorller.findScrollView(enabled: false)

        【讨论】:

          【解决方案5】:

          您也可以禁用手势识别器。

          for (UIGestureRecognizer *recognizer in pageViewController.gestureRecognizers)
          {
              recognizer.enabled = NO;
          }
          

          与流行的答案不同,它依赖于内部UIScrollView 的存在,这个使用公共gestureRecognizers 数组。如果您使用书本式分页,则底层滚动视图可能不存在。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-12-07
            • 2015-07-24
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多