捕获子视图的 mouseDown 事件。 (可可 OSX)

发布于 2024-08-19 12:01:39 字数 117 浏览 6 评论 0原文

我有一系列嵌套视图,我需要捕获 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

萌吟 2024-08-26 12:01:39

您的子视图是否定义了自己的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.

往昔成烟 2024-08-26 12:01:39

在超级视图中,如果该点位于超级视图的矩形中,则可以重写 hitTest 以返回超级视图。这将阻止鼠标事件进入任何子视图。

- (NSView *)hitTest:(NSPoint)aPoint
{
    return NSPointInRect(aPoint, self.frame) ? self : nil;
}

请注意,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.

- (NSView *)hitTest:(NSPoint)aPoint
{
    return NSPointInRect(aPoint, self.frame) ? self : nil;
}

Note that aPoint is in the superview's "frame" coordinate system, not its bounds.

独留℉清风醉 2024-08-26 12:01:39

此版本适用于转换以屏幕坐标表示的点:

NSPointInRect([view convertPoint:[[view window] convertScreenToBase:point] fromView:nil], [view bounds]);

This version works for converting a point expressed in screen coordinates:

NSPointInRect([view convertPoint:[[view window] convertScreenToBase:point] fromView:nil], [view bounds]);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文