滚动时,UIScrollView取消UIPageViewController手势
我有一个UIPageViewController
来处理我的“书”的页面。 但是,每个书页都是一个以UIScrollView
作为subview
的ViewController
。 由于contentSize
, UIScrollView
只能垂直滚动。 问题是,当用户垂直滚动滚动scrollview
,由于滚动scrollview
仍在滚动/减速,用户无法翻页。 查看这个非常简单的方法是滚动页面,然后尝试点击视图的边缘。 这通常会改变页面,并且当scrollview
不移动时它确实改变页面。 但是,当它移动时,水龙头会导致滚动视图停止移动,而不是翻页。
如果UIPageViewController
试图通过点击或平移页面以使页面转动animation使用手势来转动页面,我如何取消scrollviews
手势?
有关我想要实现的行为的示例,请查看iPhone上Twitter的官方客户端。 当Feed从滚动中减速时,可以从Timeline滑动到Discover。
一个UIScrollView
滚动事件将阻止其他UIView
animation,所以在Twitter的情况下,他们可能会取消滚动一秒钟,然后刷视图。 正如你问你的问题:
“如果UIPageViewController试图通过点击或平移页面来使页面转动animation使用手势来转动页面,我如何取消滚动视图手势?”
我会build议一个解决方法。
不要依赖于UIPageViewController
固有的UIPanGestureRecognizer
, UIPanGestureRecognizer
在页面视图中包含你自己的UIPanGestureRecognizer
,这样当在页面的适当部分检测到一个泛,并以适当的方向执行时,新的UIPanGestureRecognizer
覆盖UIPageViewController
的UIGestureRecognizer
并触发必要的行动。 具体而言,您需要:
(1)使用停止滚动animation
CGPoint offset = scrollView.contentOffset; [scrollView setContentOffset:offset animated:NO];
(2)使用编程方式翻页
- (void)setViewControllers:(NSArray *)viewControllers direction: (UIPageViewControllerNavigationDirection)direction animated: (BOOL)animated completion:(void (^)(BOOL finished))completion;
使得滚动animation被停止并且页面翻转在一个stream体平移手势内完成。
UIGestureRecognizer
类可以通过使用requireGestureRecognizerToFail:方法来设置其他手势识别器的依赖。
在你的情况下,这种方法可以这样使用:
for (UIGestureRecognizer *gestureRecognizer in pageController.gestureRecognizers) { for (ViewController *viewController in viewControllers) { for (UIGestureRecognizer *gestureRecognizerForFail in viewController.scrollView.gestureRecognizers) { [gestureRecognizerForFail requireGestureRecognizerToFail:gestureRecognizer]; } } }
我有同样的问题,我的工作方式…在我的情况下,我有PDF启用缩放。 所以我举个例子:
[scrollView setMaximumZoomScale:6]; [scrollView setMinimumZoomScale:1];
当我初始化控制器和它的scrollView,并在那之后,每当我改变方向或页面,我改变缩放以适应页面的宽度,只有当缩放“很远”
CGFloat desiredWidth = scrollView.frame.size.width/pdfRect.size.width; if (desiredWidth>[self zoomScale]) { [scrollView setZoomScale:desiredWidth animated:YES]; }
我希望它有帮助