我需要找出鼠标位置是否在NSView的矩形内。
我会使用NSPointInRect(point, rect),但我需要将矩形坐标转换为屏幕坐标,并且我不确定如何转换。任何帮助都将不胜感激!
发布于 2011-01-08 06:54:56
使用NSView的convertPoint: fromView :方法,并将fromView设置为nil,将窗口坐标中的点转换为视图的坐标系。
在将鼠标指针转换到视图的坐标系后,我将使用NSView的鼠标:inRect:方法而不是NSPointInRect,因为前者也考虑了翻转的坐标系。
或者,您可以使用Tracking Area Object。
发布于 2011-01-08 06:51:30
这样的东西对你来说应该是有效的:
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
}我个人并没有使用这么多的局部变量,但希望这能让这个例子更清晰。
发布于 2012-10-12 21:47:40
一些使用鼠标的代码:inRect:(推荐的方式;这解释了翻转的坐标)
CGPoint point = [self convertPoint:[event locationInWindow] fromView:nil];
CGRect rect = [self bounds];
if ([self mouse:point inRect:rect]) {
}https://stackoverflow.com/questions/4630509
复制相似问题