我可以使用[NSEvent mouseLocation]
来获取光标的位置,但这会给我提供屏幕坐标。当光标位于视图中时,如何获得光标相对于视图的坐标?我搜索了Apple文档,但没有找到答案。
如果这有什么不同,我会想要不断检索鼠标位置,因为它将在每次帧更新中使用。
发布于 2011-09-19 03:50:04
- (void)mouseMoved:(NSEvent *)event
{
NSPoint locationInView = [self convertPoint:[event locationInWindow]
fromView:nil];
}
另外,请确保您已启用mouseMoved事件:
[window setAcceptsMouseMovedEvents:YES];
发布于 2014-11-11 07:21:16
为了完整性,有一种直接的方法来获取窗口坐标中的鼠标位置(使用NSWindow)。根据您的窗口布局,这可能等同于视图的坐标。
NSWindow *myWindow;
NSPoint mousePos;
...
mousePos = [myWindow mouseLocationOutsideOfEventStream];
返回的坐标是窗口坐标,因此如果鼠标位于窗口的左侧/下方,则返回负值。如果鼠标位于窗口的右侧/上方,则坐标将超出窗口的大小。
发布于 2011-09-19 03:48:01
NSPoint myPoint =
[myView convertPoint:[myWindow convertScreenToBase:[NSEvent mouseLocation]]
fromView:nil];
https://stackoverflow.com/questions/7463958
复制相似问题