10

I am using a UIPageViewController with transitionStyle UIPageViewControllerTransitionStyleScroll and navigationOrientation UIPageViewControllerNavigationOrientationVertical

I also have a UIPanGestureRecognizer on the view and I want to disable page scrolling when the pan gesture is active.

I am trying to set the following when the gesture begins:

pageViewController.view.userInteractionEnabled = NO;

This seems to have no effect, or it appears to work sporadically.

The only other way I have found to do it (which works) is to set the UIPageViewController dataSource to nil while the pan gesture is running, however this causes a huge delay when resetting the dataSource.

4

5 回答 5

22

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;
        }
    }
}
于 2013-08-23T08:25:59.147 回答
3

对于那些使用 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")
      }

    }
  }
于 2015-01-10T18:29:58.527 回答
0

每个人都非常非常复杂。

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

    func enableScroll(_ enable: Bool) {
        dataSource = enable ? self : nil
    }
于 2022-02-20T13:09:23.477 回答
-1

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)

于 2018-10-16T20:50:27.027 回答
-2

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

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

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

于 2015-11-17T10:50:42.110 回答