将 NSPoint 从窗口坐标转换为视图坐标

发布于 2024-12-14 08:31:42 字数 756 浏览 0 评论 0原文

我的应用程序有一个自定义视图,显示事件的时间线。该视图包装在 NSScrollView 中以支持时间轴的水平滚动。使用通知,我实现了一种机制,该机制应该显示另一个自定义视图,当用户在时间线中单击该事件时,该视图会显示有关该事件的详细信息。下面是时间线接收到事件时处理该事件的代码:

NSEvent *anEvent = [aNotification object];
NSPoint aLocationInSelf = [self convertPoint: [anEvent locationInWindow] toView: self];

// Create callout view and display
NSRect aRect = NSMakeRect(aLocationInSelf.x, aLocationInSelf.y, 300, 200);
TimelineCallout *aCallout = [[TimelineCallout alloc] initWithFrame: aRect];
[self addSubview: aCallout];

在上面的代码中,我将事件注册的鼠标单击点从窗口坐标转换为视图(时间线)坐标。

但是,当我使用调试器单步执行此操作时,不会发生任何转换,并且 locationInSelf 显示的坐标与我从 [anEvent locationInWindow] 获取的点相同。结果,标注绘制在错误的位置或根本不可见。

我一定做错了什么,但我不知道是什么......

My application has a custom view that displays a timeline of events. This view is wrapped in an NSScrollView to support horizontal scrolling of the timeline. Using notifications, I've implemented a mechanism that should show another custom view which displays detail information about an event when the user clicks that event in the timeline. Below is the code that handles the event when it is received by the timeline:

NSEvent *anEvent = [aNotification object];
NSPoint aLocationInSelf = [self convertPoint: [anEvent locationInWindow] toView: self];

// Create callout view and display
NSRect aRect = NSMakeRect(aLocationInSelf.x, aLocationInSelf.y, 300, 200);
TimelineCallout *aCallout = [[TimelineCallout alloc] initWithFrame: aRect];
[self addSubview: aCallout];

In the above code I do a conversion of the point of the mouse click as registered by the event from window coordinates to view (timeline) coordinates.

However, when I step through this with the debugger no conversion is taking place and locationInSelf shows the same coordinates as the point I get from [anEvent locationInWindow]. As a result the callout is drawn at the wrong place or not visible at all.

I must be doing something wrong but I can't figure out what...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只等公子 2024-12-21 08:31:42

为了从窗口坐标转换为视图坐标,您必须使用:

NSPoint aLocationInSelf = [self convertPoint: [anEvent locationInWindow] fromView: nil];

这将从窗口基坐标转换为接收器坐标,因为事件不是源自特定视图。

有关详细信息,请参阅 文档

更详细的解释

转换方法有点难以理解。主要有两种一般情况(其他情况是变体):

  1. 当位于同一窗口中时,将点从一个视图转换为另一个视图
  2. 将以窗口坐标表示的点转换为视图坐标

对于第一种情况,您有两个视图( view1 和 view2) 位于同一窗口中。如果您想将 view2 中的点转换为 view1 的坐标系,代码将为:

NSPoint pointInView1 = [view1 convertPoint:pointInView2 fromView:view2];

对于第二种情况,您有一个以窗口坐标(来自事件)表示的点。由于该点是用窗口坐标表示的,因此您无需指定“来自”视图,代码将是:

NSPoint pointInView1 = [view1 convertPoint:pointInWindow fromView:nil];

这是该方法的后备行为。

In order to convert from window coordinates to view coordinates, you have to use:

NSPoint aLocationInSelf = [self convertPoint: [anEvent locationInWindow] fromView: nil];

This will convert from window base coordinates to the receiver coordinates as the event is not originated from a particular view.

For more information, refer to the documentation.

More detailed explanation:

The conversion methods are a bit tricky to understand. There mainly two general cases (the other ones are variants):

  1. Convert a point from one view to another view when they are located in the same window
  2. Convert a point expressed in window coordinate into view coordinates

For the 1st case, you have two views (view1 and view2) located in the same window. If you want to convert a point from view2 into the view1's coordinate system the code will be:

NSPoint pointInView1 = [view1 convertPoint:pointInView2 fromView:view2];

For the 2nd case, you have a point expressed in window coordinates (from the event). As the point is expressed in window coordinates, you don't specify a "from" view and the code will be:

NSPoint pointInView1 = [view1 convertPoint:pointInWindow fromView:nil];

This is the fallback behavior of the method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文