什么是计算单击鼠标的观点的正确方法
Whats the correct way to calculate point in view that mouse was clicked
我的应用程序中有一个 NSView 的自定义子类。
我想知道视图中的确切点,相对于它的原点,用鼠标点击。 (即不相对于 Window 原点,而是相对于自定义视图原点)。
我一直用这个,效果很好:
-(void)mouseDown:(NSEvent *)theEvent
{
NSPoint screenPoint = [NSEvent mouseLocation];
NSPoint windowPoint = [[self window] convertScreenToBase:screenPoint];
NSPoint point = [self convertPoint:windowPoint fromView:nil];
_pointInView = point;
[self setNeedsDisplay:YES];
}
但现在我收到一条警告,指出 convertScreenToBase 已弃用,请改用 convertRectFromScreen。但是我无法从 convertRectFromScreen 获得相同的结果,而且无论如何,我对点感兴趣,而不是矩形!
我应该使用什么来正确替换上面已弃用的代码?
提前致谢!
我用 window 制作了一个示例项目并测试了 'old' 和新场景。两种情况的结果是一样的。
您必须执行一个附加步骤:创建一个以屏幕点为原点的简单矩形。然后使用新的原点,返回的矩形。
这是新代码:
-(void)mouseDown:(NSEvent *)theEvent
{
NSPoint screenPoint = [NSEvent mouseLocation];
NSRect rect = [[self window] convertRectFromScreen:NSMakeRect(screenPoint.x, screenPoint.y, 0, 0)];
NSPoint windowPoint = rect.origin;
NSPoint point = [self convertPoint:windowPoint fromView:nil];
_pointInView = point;
[self setNeedsDisplay:YES];
}
希望对您有所帮助!
您代码中的这一行:
NSPoint screenPoint = [NSEvent mouseLocation];
鼠标光标的位置与事件流不同步。这不是你当前正在处理的事件的位置,那是过去很短的时间;它现在是光标的位置,这意味着您可能会跳过一些重要的内容。您应该几乎总是使用与事件流同步的位置。
为此,请使用您的方法接收的 theEvent
参数。 NSEvent
有一个 locationInWindow
属性,它已经被翻译成接收它的 window 的坐标。这消除了您转换它的需要。
NSPoint windowPoint = [theEvent locationInWindow];
您将 window 位置转换为视图坐标系的代码没问题。
我找到了解决方案:
NSPoint screenPoint = [NSEvent mouseLocation];
NSRect screenRect = CGRectMake(screenPoint.x, screenPoint.y, 1.0, 1.0);
NSRect baseRect = [self.window convertRectFromScreen:screenRect];
_pointInView = [self convertPoint:baseRect.origin fromView:nil];
简单地使用 convert(_:from:)
可能不准确,当事件的 window 和视图的 window 不相同时可能会发生这种情况。请检查我在另一个问题中的回答以获得更可靠的方式。
我的应用程序中有一个 NSView 的自定义子类。 我想知道视图中的确切点,相对于它的原点,用鼠标点击。 (即不相对于 Window 原点,而是相对于自定义视图原点)。
我一直用这个,效果很好:
-(void)mouseDown:(NSEvent *)theEvent
{
NSPoint screenPoint = [NSEvent mouseLocation];
NSPoint windowPoint = [[self window] convertScreenToBase:screenPoint];
NSPoint point = [self convertPoint:windowPoint fromView:nil];
_pointInView = point;
[self setNeedsDisplay:YES];
}
但现在我收到一条警告,指出 convertScreenToBase 已弃用,请改用 convertRectFromScreen。但是我无法从 convertRectFromScreen 获得相同的结果,而且无论如何,我对点感兴趣,而不是矩形!
我应该使用什么来正确替换上面已弃用的代码? 提前致谢!
我用 window 制作了一个示例项目并测试了 'old' 和新场景。两种情况的结果是一样的。
您必须执行一个附加步骤:创建一个以屏幕点为原点的简单矩形。然后使用新的原点,返回的矩形。
这是新代码:
-(void)mouseDown:(NSEvent *)theEvent
{
NSPoint screenPoint = [NSEvent mouseLocation];
NSRect rect = [[self window] convertRectFromScreen:NSMakeRect(screenPoint.x, screenPoint.y, 0, 0)];
NSPoint windowPoint = rect.origin;
NSPoint point = [self convertPoint:windowPoint fromView:nil];
_pointInView = point;
[self setNeedsDisplay:YES];
}
希望对您有所帮助!
您代码中的这一行:
NSPoint screenPoint = [NSEvent mouseLocation];
鼠标光标的位置与事件流不同步。这不是你当前正在处理的事件的位置,那是过去很短的时间;它现在是光标的位置,这意味着您可能会跳过一些重要的内容。您应该几乎总是使用与事件流同步的位置。
为此,请使用您的方法接收的 theEvent
参数。 NSEvent
有一个 locationInWindow
属性,它已经被翻译成接收它的 window 的坐标。这消除了您转换它的需要。
NSPoint windowPoint = [theEvent locationInWindow];
您将 window 位置转换为视图坐标系的代码没问题。
我找到了解决方案:
NSPoint screenPoint = [NSEvent mouseLocation];
NSRect screenRect = CGRectMake(screenPoint.x, screenPoint.y, 1.0, 1.0);
NSRect baseRect = [self.window convertRectFromScreen:screenRect];
_pointInView = [self convertPoint:baseRect.origin fromView:nil];
简单地使用 convert(_:from:)
可能不准确,当事件的 window 和视图的 window 不相同时可能会发生这种情况。请检查我在另一个问题中的回答以获得更可靠的方式。