向UIScrollView转发触摸 [英] Forward touches to a UIScrollView

查看:244
本文介绍了向UIScrollView转发触摸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个视图控制器。视图控制器A有一个 UIScrollView 并提供视图控制器B.该演示是交互式的,并由 scrollView.contentOffset 控制。

I have two view controllers. View controller A has a UIScrollView and presents view controller B. The presentation is interactive and controlled by the scrollView.contentOffset.

我想集成一个交互式的dismiss转换:当向上平移时,ViewController B应该被交互式地关闭。

I want to integrate an interactive dismiss transition: When panning up, ViewController B should be dismissed interactively. The interactive dismiss transition should also control the ViewController A scrollView.

我的第一次尝试是使用 UIPanGestureRecognizer scrollView.contentOffset 根据平移距离。这是可行的,但是当平移手势结束时,scrollView偏移必须动画到结束位置。使用 - [UIScrollView setContentOffset:animated:不是一个好的解决方案,因为它使用线性动画,不考虑当前锅速度,并不能很好地减速。

My first attempt was using a UIPanGestureRecognizer and setting the scrollView.contentOffset according to the panned distance. This works, however when the pan gesture is ended, the scrollView offset has to be animated to the end position. Using -[UIScrollView setContentOffset:animated: is not a good solution since it uses a linear animation, doesn't take the current pan velocity into account and doesn't decelerate nicely.

所以我认为应该可以将触摸事件从我的平移手势识别器馈送到滚动视图。

So I thought it should be possible to feed the touch events from my pan gesture recognizer into the scroll view. This should give us all the nice scroll view animation behavior.

我尝试覆盖 -touchesBegan / Moved / Ended / Cancelled withEvent:方法在我的 UIPanGestureRecognizer 子类如下:

I tried overriding the -touchesBegan/Moved/Ended/Cancelled withEvent: methods in my UIPanGestureRecognizer subclass like this:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [scrollView touchesBegan:touches withEvent:event];
    [scrollView.panGestureRecognizer touchesBegan:touches withEvent:event];

    [super touchesBegan:touches withEvent:event];
}


$ b <跟踪模式。 (它会去 dragging = YES 但是这是关于它。)我验证scrollView是 userInteractionEnabled

But apparently something is blocking the scroll view from entering tracking mode. (It does go to dragging = YES but that's about it.) I verified the scrollView is userInteractionEnabled, not hidden and added to the view hierarchy.

那么如何将我的触摸事件转发到 UIScrollView

So how can I forward my touch events to UIScrollView?

推荐答案

阅读有趣的答案后, UIScrollView 的事件流,我得出结论,试图从手势识别器远程控制滚动视图可能很难实现,因为触摸是突变的,而路由到视图和手势识别器。由于 UITouch 不符合 NSCopying ,我们也无法克隆触摸事件,以便稍后以未修改的方式发送状态。

After reading an interesting answer describing UIScrollView's event flow, I came to the conclusion that trying to "remote control" a scroll view from a gesture recognizer is probably very hard to achieve because touches are mutated while being routed to views and gesture recognizers. Since UITouch doesn't conform to NSCopying we also can't clone touch events in order to send them later in unmodified state.

虽然没有真正解决我问的问题,我发现了一个解决方法来完成我需要的。我只是添加了一个滚动视图来查看控制器B,并与VC A的滚动视图(垂直滚动时添加到视图层次结构)同步:

While not really solving the problem I asked for, I found a workaround to accomplish what I need. I just added a scroll view to view controller B and synced it with VC A's scroll view (which is added to the view hierarchy when vertically scrolling):

// delegate of VC B's scrollView
- (void)scrollViewDidScroll:(UIScrollView*)scrollView
    scrollViewA.contentOffset = scrollView.contentOffset;
}

感谢Friedrich Markgraf谁想出了想法

Thanks to Friedrich Markgraf who came up with the idea.

这篇关于向UIScrollView转发触摸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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