问题描述
我希望能够创建我自己的 UIView 容器子类,它可以在子视图之前先对触摸做出反应.这很棘手,因为通常子视图在超级视图之前接收触摸事件(通过 touchesBegan:
等).UIScrollView 是如何反转这个的?
I'd like to be able to create my own container subclasses of UIView which can react first to touches, before their subviews. This is tricky because normally a subview receives touch events (via touchesBegan:
etc) before superviews. How does UIScrollView reverse this?
明确地说,我不是询问 UIScrollView 的行为如何.我了解它的作用,以及您通常如何使用它.我在问我如何才能干净地实现我自己的版本 - 不是因为我想要,而是因为我正在尝试构建利用类似行为的可重用容器视图.
To be clear, I am not asking how UIScrollView behaves. I understand what it does, and how you would normally use it. I'm asking about how I could cleanly implement my own version of this -- not because I want to, but because I'm trying to build reusable container views that take advantage of similar behavior.
推荐答案
您可以在 UIView
子类中实现 hitTest:withEvent:
方法.
You can implement hitTest:withEvent:
method in your UIView
subclass.
调用此方法来检查必须接收触摸事件的子视图,因此您可以在它实际发生之前在那里执行一些操作.您还可以更改必须接收事件的子视图.
This method gets called to check what subview must receive the touch event, so you can perform some action there before it actually happens. You can also change subview that must receive event.
这篇关于UIScrollView 如何从其子视图中窃取触摸?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!