捕获子视图的 mouseDown 事件。 (可可 OSX)
我有一系列嵌套视图,我需要捕获 mouseDown 事件,并在选择这些视图中的任何一个时执行相同的操作。有没有办法告诉超级视图处理其子视图的事件?处理此问题的最佳方法是将透明视图放在所有其他视图之上并让该视图处理事件吗?
I have a series of nested views and I need to catch the mouseDown event ant do the same thing when any of these views are selected. Is there a way to tell a superview to handle events for its subviews? Is the best way to handle this to put a transparent view on top of all my other views and have this view handle the events?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的子视图是否定义了自己的
mouseDown:
?如果他们还没有定义自己的
-[... mouseDown:(id)event]
,那么他们应该已经将他们的事件传递到响应者链上,这应该到达你的超级视图。否则,(除了他们需要做的任何其他处理之外)他们需要决定超级视图还应该接收哪些事件并为这些事件调用
[super mouseDown:event]
。Do your subviews define their own
mouseDown:
?If they do not already define their own
-[… mouseDown:(id)event]
, then they should already be passing their events up the responder chain, which should get to your superview.Otherwise, (in addition to whatever other handling they need to do) they need to decide which events the superview should also receive and call
[super mouseDown:event]
for those events.在超级视图中,如果该点位于超级视图的矩形中,则可以重写 hitTest 以返回超级视图。这将阻止鼠标事件进入任何子视图。
请注意,aPoint 位于超级视图的“框架”坐标系中,而不是其边界中。
In the superview, you can override hitTest to return the superview if the point is in the superview's rectangle. That will prevent the mouse event from going to any of the subviews.
Note that aPoint is in the superview's "frame" coordinate system, not its bounds.
此版本适用于转换以屏幕坐标表示的点:
This version works for converting a point expressed in screen coordinates: