macos 如何确定鼠标是否在视图上

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4630509/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 07:34:07  来源:igfitidea点击:

How to find if the mouse is over a view

objective-ccocoamacosnsview

提问by Arlen Anderson

I need to find if the mouse position is inside an NSView's rect.

我需要找到鼠标位置是否在 NSView 的矩形内。

I'd use NSPointInRect(point, rect), but I'd need to convert the rect coordinates to screen coordinates and I'm not sure how. Any help would be much appreciated!

我会使用NSPointInRect(point, rect),但我需要将矩形坐标转换为屏幕坐标,但我不确定如何转换。任何帮助将非常感激!

回答by Jon Steinmetz

Something like this should work for you:

像这样的事情应该适合你:

NSView* myView; // The view you are converting coordinates to
NSPoint globalLocation = [ NSEvent mouseLocation ];
NSPoint windowLocation = [ [ myView window ] convertScreenToBase: globalLocation ];
NSPoint viewLocation = [ myView convertPoint: windowLocation fromView: nil ];
if( NSPointInRect( viewLocation, [ myView bounds ] ) ) {
    // Do your thing here
}

I don't personally use this many local variables but hopefully this make this example clearer.

我个人不使用这么多局部变量,但希望这能让这个例子更清晰。

回答by Matt Bierner

Use NSView's convertPoint:fromView:method with fromViewas nilto covert a point in window coordinates to a view's coordinate system.

使用 NSView 的convertPoint:fromView:方法和fromView作为nil将窗口坐标中的点转换为视图的坐标系。

After converting the mouse point to the view's coordinate system, I would use NSView's mouse:inRect:method instead of NSPointInRect as the former also accounts for flipped coordinate systems.

将鼠标点转换为视图的坐标系后,我将使用 NSView 的mouse:inRect:方法而不是 NSPointInRect,因为前者也考虑了翻转坐标系。

Alternatively, you could use a Tracking Area Object.

或者,您可以使用Tracking Area Object

回答by aepryus

Some code making use of mouse:inRect: (the recommended way; that accounts for flipped coordinates)

一些使用 mouse:inRect 的代码:(推荐的方式;考虑翻转坐标)

CGPoint point = [self convertPoint:[event locationInWindow] fromView:nil];
CGRect rect = [self bounds];
if ([self mouse:point inRect:rect]) {
}

回答by Ryan H

The NSView isMousePointfunction works for me, without having to worry about anything CG.

NSView isMousePoint函数对我有用,无需担心任何 CG。

func isMouseInView(view: NSView) -> Bool? {
    if let window = view.window {
        return view.isMousePoint(window.mouseLocationOutsideOfEventStream, in: view.frame)
    }
    return nil
}

回答by eonist

None of these answers take into account that a view might not be rectangular.

这些答案都没有考虑到视图可能不是矩形。

Here is an universal method that works on non-rectangular views as well. Think watch-face or a complex path shape.

这是一个适用于非矩形视图的通用方法。想想表盘或复杂的路径形状。

This code snippet will give you a localPoint from a globalPoint (aka globalToLocal). (GlobalPoint in 0,0 window space) If you change the argument to "toView" then you get a "localToGlobal" point.

此代码片段将为您提供来自 globalPoint(又名 globalToLocal)的 localPoint。(0,0 窗口空间中的 GlobalPoint)如果将参数更改为“toView”,那么您将获得“localToGlobal”点。

let localPoint = convertPoint(aPoint, fromView: self.window?.contentView)    

The bellow code would return true or false if the point was inside the path or not.

如果点是否在路径内,波纹管代码将返回 true 或 false。

CGPathContainsPoint(someCGPath,nil,localPoint,true)

CGPathContainsPoint(someCGPath,nil,localPoint,true)

NOTE:aPoint is a globalPoint derived from the parameter in hitTest, it can also be derived from:

注意:aPoint 是从 hitTest 中的参数派生的 globalPoint,它也可以派生自:

(self.window?.mouseLocationOutsideOfEventStream)!

NOTE: There might be a more correct way of doing this. But this works,and doesn't care about flipped views.

注意:可能有更正确的方法来做到这一点。但这有效,并且不关心翻转视图。