将 NSPoint 从窗口坐标转换为视图坐标
我的应用程序有一个自定义视图,显示事件的时间线。该视图包装在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了从窗口坐标转换为视图坐标,您必须使用:
这将从窗口基坐标转换为接收器坐标,因为事件不是源自特定视图。
有关详细信息,请参阅 文档。
更详细的解释:
转换方法有点难以理解。主要有两种一般情况(其他情况是变体):
对于第一种情况,您有两个视图( view1 和 view2) 位于同一窗口中。如果您想将 view2 中的点转换为 view1 的坐标系,代码将为:
对于第二种情况,您有一个以窗口坐标(来自事件)表示的点。由于该点是用窗口坐标表示的,因此您无需指定“来自”视图,代码将是:
这是该方法的后备行为。
In order to convert from window coordinates to view coordinates, you have to use:
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):
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:
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:
This is the fallback behavior of the method.