2012-11-14 84 views
9

我使用的是UIPageViewController與transitionStyle UIPageViewControllerTransitionStyleScroll和navigationOrientation UIPageViewControllerNavigationOrientationVerticalUIPageViewController禁用滾動

我也對視圖UIPanGestureRecognizer,我想禁用頁面滾動時的移動手勢被激活。

我想,當手勢開始設置如下:

pageViewController.view.userInteractionEnabled = NO; 

這似乎沒有任何效果,或出現零星工作。

我發現做的唯一的另一種方式(工作)是在平移手勢運行時將UIPageViewController數據源設置爲零,但是這會在重置數據源時導致巨大的延遲。

回答

23

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; 
     } 
    } 
} 
+0

我一直在尋找一段時間,這是我發現的禁用UIPageViewController上的滑動操作,但保持水龍頭移動的最佳答案。 –

+0

在swift中,如果你喜歡濃縮的東西有點難以閱讀:'pageViewController.view.subviews.flatMap({$ 0 as?UIScrollView})。forEach({$ 0.isScrollEnabled = enabled})' – HHK

3

對於那些誰使用,而不是迅速的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") 
     } 

    } 
    } 
0

這個怎麼樣?

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