NSClickGestureRecognizer.location(in:) 和 hitTest()

NSClickGestureRecognizer.location(in:) and hitTest()

我有以下看法:

它有一个基于 NSVisualEffectView 的自定义 class,并且包含一个图像视图、一个标签 (NSTextField) 和一个弹出按钮。自定义视图的 isFlipped 总是 false.

自定义视图还包含一个使用委托的 NSClickGestureRecognizer。委托只实现一种方法:

func gestureRecognizerShouldBegin(_ gestureRecognizer: NSGestureRecognizer) -> Bool {
    let thePoint = gestureRecognizer.location(in: view)

    if let theView = view.hitTest(thePoint) {
        return !theView.handlesMouseEvents
    }
    else {
        return true
    }
}

如果我在弹出菜单的中间单击,location(in:) returns 例如值 (182, 16)。对于非翻转视图,这对我来说似乎是正确的。但是 hitTest() returns 结果是该点的背景视图,而不是弹出按钮。

我做错了什么?

如果我使用手动翻转点 (y := height - y) 进行命中测试,我会得到弹出按钮作为结果。但我不想使用那种方法,因为它对我来说很难看。

如果我使用 window 的内容视图进行命中测试,似乎可以正常工作。但是我还是想知道为什么显示的方法不起作用。

hitTest(_:)的参数point

A point that is in the coordinate system of the view’s superview, not of the view itself.

解决方案:在父视图坐标中传递一个点。