有没有办法获得坐标相对于最内层NSView当父不是窗口? [英] Is there a way to get coordinates relative to the innermost NSView when the parent is not the window?

查看:123
本文介绍了有没有办法获得坐标相对于最内层NSView当父不是窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有关于NSView的问题:

I have question about NSView:

想象一个自定义视图,其中mouseDown,mouseDrag和mouseUp方法被覆盖,因此用户可以拖动一个点屏幕。要拖动它,我需要相对于当前视图的鼠标坐标。当视图的父视图是窗口时,这不是一个问题,但是当视图在另一个视图中时,如何获取它们?

Imagine a Custom View where the mouseDown, mouseDrag and mouseUp methods are overriden so the user can drag a point (NSRect) on the screen. To drag it I need the mouse coordinates relative to the current view. This is not a problem when the parent of the view is the window, but how do I get them when the view is inside another view?

@implementation MyView

- (id)initWithFrame:(NSRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        pointXPosition = 200.0f;
        pointYPosition = 200.0f;

        locked = NO;
    }
    return self;
}

- (void) drawRect:(NSRect)rect {

    NSRect point = NSMakeRect(pointXPosition, pointYPosition, 6.0f, 6.0f);
    [[NSColor redColor] set];
    NSRectFill(point);

}

- (void)mouseDown:(NSEvent *)theEvent {
    NSPoint mousePos = [theEvent locationInWindow];
    NSRect frame = [super frame];
    CGFloat deltaX = mousePos.x - frame.origin.x - pointXPosition;
    CGFloat deltaY = mousePos.y - frame.origin.y - pointYPosition;
    if(sqrtf(deltaX * deltaX + deltaY * deltaY) < 100.0f)
        locked = YES;
}

- (void)mouseUp:(NSEvent *)theEvent {
    locked = NO;
}

- (void)mouseDragged:(NSEvent *)theEvent {

    if(locked) {
        NSPoint mousePos = [theEvent locationInWindow];

        NSRect frame = [super frame];

        CGFloat oldXPos = pointXPosition;
        CGFloat oldYPos = pointYPosition;

        pointXPosition = mousePos.x - frame.origin.x;
        pointYPosition = mousePos.y - frame.origin.y;

        CGFloat rectToDisplayXMin = MIN(oldXPos, pointXPosition);
        CGFloat rectToDisplayYMin = MIN(oldYPos, pointYPosition);

        CGFloat rectWidthToDisplay = MAX(oldXPos, pointXPosition) - rectToDisplayXMin;
        CGFloat rectHeigthToDisplay = MAX(oldYPos, pointYPosition) - rectToDisplayYMin;

        NSRect dirtyRect = NSMakeRect(rectToDisplayXMin,
                                      rectToDisplayYMin,
                                      rectWidthToDisplay + 6.0f,
                                      rectHeigthToDisplay + 6.0f);

        [self setNeedsDisplayInRect:dirtyRect];
    }
}


推荐答案

不需要手动转换到本地坐标系。您可以通过将 convertPoint:fromView:消息发送到您的视图,将点转换为局部坐标系。发送 nil 作为参数 fromView 将会转换来自视图的父窗口(无论在哪里)的点。您也可以发送任何其他视图,以获得从该空间转换的坐标:

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];

这篇关于有没有办法获得坐标相对于最内层NSView当父不是窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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