将NSPoint从窗口坐标转换为视图坐标 [英] Converting an NSPoint from window coordinates to view coordinates

查看:452
本文介绍了将NSPoint从窗口坐标转换为视图坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

//创建callout视图并显示
NSRect aRect = NSMakeRect(aLocationInSelf.x,aLocationInSelf.y,300,200);
TimelineCallo * aCallout = [[TimelineCallo alloc] initWithFrame:aRect];
[self addSubview:aCallout];

在上面的代码中,我做一个转换鼠标点击的事件



但是,当我使用调试器完成这一步时,不会发生转换, locationInSelf 显示与从 [anEvent locationInWindow] 获得的点相同的坐标。因此,标注会在错误的地方绘制,或根本不可见。



我必须做错事,但我不知道什么...

解决方案

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

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

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



有关详情,请参阅文档



详细说明



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


  1. 将点从一个视图转换到另一个视图



  2. 对于第一种情况,将窗口坐标中表示的点转换为视图坐标
    < ,您必须查看位于同一窗口中的(view1和view2)。如果你想把一个点从view2转换到view1的坐标系,代码将是:

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

    对于第二种情况,你有一个以窗口坐标由于该点以窗口坐标表示,因此不指定from视图,代码将为:

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

    这是方法的回退行为。


    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...

    解决方案

    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 to view (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.

    这篇关于将NSPoint从窗口坐标转换为视图坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆