如何检测“着陆”在 UIScrollView 的超级视图中?
我有一个包含 UIScrollView
的 UIView
,并且我希望能够在用户任何时候捕获 UIView
中的“Touch Down”事件点击 UIScrollView
。
我尝试在 UIViewController
中包含所有 TouchBegan/Ended/Cancelled 处理程序,但是当点击主 UIView< 中包含的
UIScrollView
内部时,它们都不会被触发。 /代码>。
实现这一目标的最佳方法是什么?
I have a UIView
that contains a UIScrollView
and I want to be able to capture the "Touch Down" event in the UIView
any time the user taps on the UIScrollView
.
I've tried including all the touchesBegan/Ended/Cancelled handlers in my UIViewController
but none of them get fired when tapping inside the UIScrollView
contained in the main UIView
.
What is the best way to accomplish this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在 UIView 中,实现 TouchBegan:
旁注:确保 UIView 中的 userInteractionEnabled 设置为 YES。
In the UIView, implement touchesBegan:
A side note: make sure userInteractionEnabled is set to YES in the UIView.
您还可以在 UIView 子类中实现
hitTest:withEvent:
。调用此方法以确定哪个子视图应接收触摸事件。因此,在这里您可以只跟踪通过您的视图的所有事件,也可以隐藏子视图中的某些事件。在这种情况下,您可能不需要禁用滚动视图的用户交互。有关此方法的更多详细信息,请参阅
UIView
类参考。You can also implement
hitTest:withEvent:
in your UIView subclass. This method gets called to determine which subview should receive touch event. So here you can either just track all events passing through your view not or hide some of the events from subviews. In this case you may not need to disable user interaction for your scrollview.See more details on this method in
UIView
class reference.您还可以将手势识别器添加到您的超级视图中。
例如,如果您需要激活/停用诸如覆盖滚动视图的按钮之类的内容,请使用点击手势:
手势确实会保留滚动视图交互
You can also add a gesture recognizer to your superview.
For example a tap gesture if you need to activate/deactivate things like buttons overlaying the scroll view :
Gestures do preserve the scroll view interaction
您需要禁用用户与滚动视图的交互...
一旦禁用,UIScrollView 的超级视图就会获取 TouchBegan 事件。
You need to disable user interaction with the scroll view as such ...
Once disabled, the UIScrollView's superview gets the touchesBegan event.