多个视图处理触摸事件
我在 UITableView
之上有一个 UIView
的子类。我正在使用 UITableView 来显示一些数据,同时我想叠加一个跟随手指的动画(例如,留下痕迹)。
如果我做对了,我需要 UIView 子类和 UITableView 来处理触摸事件。我怎样才能做到这一点? 是否可以在 UIView 子类上触发 touchesMoved
,然后在 UITableView
上触发?
非常感谢您的帮助。
I have a subclass of UIView
on top of a UITableView
. I am using the UITableView
to display some data and, at the same time, I would like to overlay an animation that follows the finger (for instance, leaving a trail).
If I get it right, I need the touch events to be handled both by the UIView
subclass and the UITableView
. How can I do that?
Is it possible to have, ie, touchesMoved
being triggered on the UIView subclass and then on UITableView
?
Thank you so much for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我解决这个问题的方法不是那么干净,但是有效。请让我知道是否有更好的方法来做到这一点。
我已经重写了自定义
UIView
的hitTest
,以便它将触摸定向到下面的UITableView
。然后在UITableView
中,我通过touchesBegan
、touchesMoved
等处理手势。在那里我还调用touchesBegan
在UIView
上。通过这种方式,触摸由两个视图处理。
我不采取相反方式的原因(让
UIView
的touchesBegan
调用UITableView
的touchesBegan
)是UITableView
上的手势识别器无法工作。UIView
子类的hitTest
UITableView
子类的touchesBegan
The way I have solved this problem is in a way that is not that clean, but it works. Please let me know if there's a better way to do this.
I have overridden
hitTest
for my customUIView
so that it directs touches to theUITableView
underneath. Then in theUITableView
I am handling the gestures throughtouchesBegan
,touchesMoved
, etc. There I am also callingtouchesBegan
on theUIView
.In this way touches are handled by two views.
The reason why I am not doing the other way around (having
UIView
'stouchesBegan
callingUITableView
'stouchesBegan
) is that gestures recognizers on theUITableView
would not work.UIView
subclass'hitTest
UITableView
subclass'stouchesBegan
不,你不能隐式地这样做。 事件传递章节说
因此,当窗口发现触摸视图时,它返回 YES。当前只有一个视图可以处理触摸。
但是如果你需要处理 UITableView 的事件,那么就为 UIView 处理它!您可以使用 –convertPoint, -convertRect 函数,将子视图添加到 UITableView 并根据坐标移动它,还有很多其他的事情。
No, you cann't do it implicity. Event Delivery chapter says
So, when window finds touched view it returns YES. Only one view can handle touches at the current moment.
But if you need to handle event for UITableView then handle it for UIView! You can convert touched point to required coordinates with – convertPoint, – convertRect functions, add subview to UITableView and move it depends on coordinate, and a lot of another things.
UITableView 将未处理的触摸事件中继到 UIView。 (谷歌“响应者链”)
UITableView 文档
因此,您可以仅处理 UIView 中的触摸事件。所以。在你的 UIView 中
touchesstart - 执行初始化操作
touchesmove - 在 UIView 上绘制尾部(使用计时器/延迟响应来禁用点,使其看起来像一条轨迹)
touchesend - 执行剩余的操作
希望这会有所帮助。
UITableView relays unhandled touch events to UIView. (Google "responder chain")
UITableView Documentation
So, you can handle your touch events in UIView only. So. In your UIView
touchesstart - do initialization stuff
touchesmove - draw tail on UIView (Use timers/delayedresponse to desable points so that it would look like a trail)
touchesend - do remaining stuff
Hope this helps.