问题描述
我有一个 UIPageViewController
来处理我的书翻页。但是,每个图书页面都是 ViewController
,其中 UIScrollView
作为子视图
。由于 contentSize
, UIScrollView
只能垂直滚动。问题是,当用户垂直滚动 scrollview
时,由于 scrollview
仍在滚动/减速,用户无法翻页。一个非常简单的方法是滚动页面,然后尝试点击视图的边缘。这通常会更改页面,并且当 scrollview
未移动时,它确实会更改页面。但是,当它移动时,水龙头会使滚动视图停止移动而不是翻页。
如果 UIPageViewController
scrollviews 手势>是否试图通过点击或平移页面来使用手势来翻页以导致翻页动画?
有关我想要实现的行为的示例,检查iPhone上的Twitter官方客户端。
从滚动开始减速时,可以从时间轴滑动到发现。
A UIScrollView
滚动事件将阻止其他 UIView
动画,所以在Twitter的情况下,他们可能会在一瞬间取消滚动滑动视图。正如您在问题中提到的那样:
如果UIPageViewController尝试使用手势通过点击来翻页,如何取消滚动视图手势或平移页面以使页面转动动画?
我建议解决方法。
不要依赖 UIPageViewController
的固有 UIPanGestureRecognizer
,而是包含您自己的 UIPanGestureRecognizer
在页面视图中,以便在页面的相应部分中检测到平移并在适当的方向上执行时,新的 UIPanGestureRecognizer
将覆盖 UIPageViewController
的 UIGestureRecognizer
s并触发必要的操作。具体来说,你需要:
(1)暂停滚动动画
CGPoint offset = scrollView.contentOffset;
[scrollView setContentOffset:offset animated:NO];
(2)使用
- (void)setViewControllers:(NSArray *)viewControllers方向:
(UIPageViewControllerNavigationDirection)方向动画:
(BOOL)动画完成:(void(^)( BOOL完成))完成;
以便停止滚动动画并在一个流体平移手势内完成翻页。 / p>
I have a UIPageViewController
that handles turning the pages of my "book". However, each book page is a ViewController
with a UIScrollView
as a subview
. The UIScrollView
is only able to scroll vertically due to the contentSize
. The problem is that while the user scrolls the scrollview
vertically, as the scrollview
is still scrolling/decelerating the user can not turn the page. A very easy way to see this is to scroll the page and then try to tap the edge of the view. This would normally change the page, and it does change the page when the scrollview
is not moving. However, when it is moving, the tap causes the scrollview to stop moving instead of turn the page.
How do I cancel the scrollviews
gestures if the UIPageViewController
is trying to use the gesture to turn the page by tapping or panning the page to cause the page turn animation?
For an example of the behavior I want to achieve, check the Twitter's official client on the iPhone.It's possible to swipe from Timeline to Discover while the feed is still decelerating from scrolling.
A UIScrollView
scroll event will block other UIView
animations, so in the case of Twitter, they're probably canceling the scroll a split second before swiping the view. As you've asked in your question:
"How do I cancel the scrollviews gestures if the UIPageViewController is trying to use the gesture to turn the page by tapping or panning the page to cause the page turn animation?"
I'll suggest a workaround.
Instead of relying on the UIPageViewController
's inherent UIPanGestureRecognizer
, include your own UIPanGestureRecognizer
in the page view so that when a pan is detected in the appropriate section of the page and performed in the appropriate direction, that new UIPanGestureRecognizer
overrides the UIPageViewController
's UIGestureRecognizer
s and triggers the necessary actions. Specifically, you need to:
(1) Halt the scrolling animation using
CGPoint offset = scrollView.contentOffset;
[scrollView setContentOffset:offset animated:NO];
(2) Turn the page programmatically using
- (void)setViewControllers:(NSArray *)viewControllers direction:
(UIPageViewControllerNavigationDirection)direction animated:
(BOOL)animated completion:(void (^)(BOOL finished))completion;
so that both the scrolling animation is halted and the page flip is completed within one fluid pan gesture.
这篇关于UIScrollView在滚动时取消UIPageViewController手势的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!