嵌套的UIScrollViews同时滚动 [英] Nested UIScrollViews scrolling simultaneously

查看:245
本文介绍了嵌套的UIScrollViews同时滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个嵌套的UIScrollViews:父级仅限于水平分页,而子级仅限于垂直滚动。内容是一个可以自由拖动的大视图,但是可以捕捉到三个水平部分中的一个。嵌套滚动视图的默认行为是仅允许一次向一个方向滚动,但我希望允许同时向两个方向拖动以保持操纵单个大视图的感觉。

I have two nested UIScrollViews: the parent limited to horizontal paging and the child limited to vertical scrolling. The content is one large view that can be freely dragged around, but snaps to one of three horizontal sections. The default behavior of the nested scroll views is to only allow scrolling in one direction at a time, but I wanted to allow simultaneous dragging in both direction to maintain the feeling of manipulating a single large view.

我目前的解决方案涉及隔离垂直滚动视图的手势并将其委托设置为我的视图控制器:

My present solution involved isolating the vertical scroll view's gesture and setting its delegate to my view controller:

for (UIGestureRecognizer *gesture in scrollView.gestureRecognizers)
    if ([gesture isKindOfClass:[UIPanGestureRecognizer class]])
        gesture.delegate = self;

然后,我实现了委托方法,允许分页视图的手势与pan同时识别滚动视图的手势:

Then, I implemented the delegate method to allow the gestures of the paging view to recognize simultaneously with the pan gesture of the scroll view:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    if (gestureRecognizer.view == scrollView && otherGestureRecognizer.view == pageView)
        return YES; // allow simultaneous scrolling of pageView and scrollView

    return NO;
}

此解决方案主要有效,但拖动视图时偶尔会出现故障周围,​​特别是当我用鼠标快速移动它或将其拖过视图边界时。具体来说,其中一个滚动视图将暂时跳回到它开始的位置,好像该手势已被取消,但如果我继续滚动它会跳回来。

This solution mostly works, but it will occasionally act glitchy when I drag the view around, particularly when I move it around quickly with mouse or drag it around past the view bounds. Specifically, one of the scroll views will temporarily jump back to where it started as if that gesture had been canceled, but then it will jump back if I keep scrolling.

什么我想知道是否有更简单或更可靠的方法来实现这样的滚动,我忽略了,或者我可以做些什么来消除毛刺行为。

What I want to know is if there is a simpler or more reliable method to achieve scrolling like this that I've overlooked, or if there's anything that I can do to eliminate the glitchy behavior.

推荐答案

当视图被拖出内容区域,释放,然后在滚动视图反弹之前再次点击/拖动时,会发生毛刺行为。例如,当视图由几个小的滑动滚动时,可能会发生这种情况。其中一个滚动视图会混淆并尝试减速(反弹)同时被拖动,导致它在原点和它被拖动到的位置之间来回抖动。

The glitchy behavior was happening when the views were dragged out of the content area, released, and then tapped/dragged again before the scroll views bounced back. This could happen, for example, when the view was scrolled by several small swipes. One of the scroll views would get confused and try to decelerate (bounce) while simultaneously being dragged, causing it to jitter back and forth between the origin and where it had been dragged to.

我能够通过反转滚动视图的嵌套(垂直滚动视图内的分页视图)并将委托添加到分页视图的UIPanGestureRecognizer而不是滚动视图的手势来解决这个问题。现在它自然滚动,好像它是单个滚动视图,同时仍然只符合水平方向的分页。我认为滚动视图不是像这样被同时滚动进行滚动,所以我不确定原始的错误行为是否是错误的结果,或者只是意外做某事的后果。

I was able to fix this by reversing the nesting of the scroll views (paging view inside of the vertical scrolling view) and by adding the delegate to the paging view's UIPanGestureRecognizer instead of to the scrolling view's gesture. Now it scrolls naturally as if it was a single scroll view while still conforming to paging only in the horizontal direction. I don't think it was intended for scroll views to be tricked into scrolling simultaneously like this, so I'm not sure if the original glitchy behavior was the result of a bug, or just a consequence of doing something unintended.

这篇关于嵌套的UIScrollViews同时滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆