Forward touches to a UIScrollView(向前触摸到 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
.
我想集成一个交互式关闭过渡:向上平移时,应以交互方式关闭 ViewController B.交互式关闭过渡还应控制 ViewController A scrollView.
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
.这可行,但是当平移手势结束时,滚动视图偏移必须动画到结束位置.使用 -[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.
我尝试在我的 UIPanGestureRecognizer
子类中覆盖 -touchesBegan/Moved/Ended/Cancelled withEvent:
方法,如下所示:
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];
}
但显然有些东西阻止了滚动视图进入 tracking
模式.(确实是 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:向前触摸到 UIScrollView
基础教程推荐
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01