Arl*_*son 19 macos cocoa objective-c nsview
我需要找到鼠标位置是否在NSView的rect内.
我会用NSPointInRect(point, rect)
,但我需要将rect坐标转换为屏幕坐标,我不知道如何.任何帮助将非常感激!
Jon*_*etz 33
这样的事情对你有用:
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
}
Run Code Online (Sandbox Code Playgroud)
我个人并没有使用这么多局部变量,但希望这会使这个例子更加清晰.
NSView isMousePoint函数对我有用,无需担心任何 CG。
func isMouseInView(view: NSView) -> Bool? {
if let window = view.window {
return view.isMousePoint(window.mouseLocationOutsideOfEventStream, in: view.frame)
}
return nil
}
Run Code Online (Sandbox Code Playgroud)
一些使用鼠标的代码:inRect :(推荐方式;考虑翻转坐标)
CGPoint point = [self convertPoint:[event locationInWindow] fromView:nil];
CGRect rect = [self bounds];
if ([self mouse:point inRect:rect]) {
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
10892 次 |
最近记录: |