Is there a way to get coordinates relative to the innermost NSView when the parent is not the window?

前端 未结 1 482
北荒
北荒 2021-02-07 11:56

I have question about NSView:

Imagine a Custom View where the mouseDown, mouseDrag and mouseUp methods are overriden so the user can drag a point (NSRect) on the screen.

1条回答
  •  一向
    一向 (楼主)
    2021-02-07 12:09

    You don't need to convert to the local coordinate system manually. You can convert the point to the local coordinate system by sending the convertPoint:fromView: message to your view. Sending nil as the parameter to fromView will convert the point from the view's parent window (wherever that is). You can also send any other view to get the coordinates converted from that space as well:

    // convert from the window's coordinate system to the local coordinate system
    NSPoint clickPoint = [self convertPoint:[theEvent locationInWindow] fromView:nil];
    
    // convert from some other view's cooridinate system
    NSPoint otherPoint = [self convertPoint:somePoint fromView:someSuperview];
    

    0 讨论(0)
提交回复
热议问题