Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Cocoa 在NSWindow中的鼠标上获取子视图的NSPoint_Cocoa_Subview_Mouseup - Fatal编程技术网

Cocoa 在NSWindow中的鼠标上获取子视图的NSPoint

Cocoa 在NSWindow中的鼠标上获取子视图的NSPoint,cocoa,subview,mouseup,Cocoa,Subview,Mouseup,我有一个应用程序,我正在创建主窗口的两个子视图,子视图是一个名为Page的类,在每个子视图上,我放置另一个名为Ad的类的子视图。我正在页面子视图之间拖动Ad类。起初我只是重置Ad类视图的框架,但我真正想做的是将其放置在发生NSLeftMouseUp事件的位置 我这样做的过程是在创建页面子视图时将它们注册到一个数组中。然后我创建了NSWindow的子类,并将该类分配给IB中的主窗口。我将页面视图数组传递给该类 我认为应该覆盖sendEvent方法,检查事件是NSLeftMouseDown、nsLe

我有一个应用程序,我正在创建主窗口的两个子视图,子视图是一个名为Page的类,在每个子视图上,我放置另一个名为Ad的类的子视图。我正在页面子视图之间拖动Ad类。起初我只是重置Ad类视图的框架,但我真正想做的是将其放置在发生NSLeftMouseUp事件的位置

我这样做的过程是在创建页面子视图时将它们注册到一个数组中。然后我创建了NSWindow的子类,并将该类分配给IB中的主窗口。我将页面视图数组传递给该类

我认为应该覆盖sendEvent方法,检查事件是NSLeftMouseDown、nsLeftMouseDraged还是NSLeftMouseUp。NSLeftMouseDown用于检查单击的子视图是否是窗口层次结构中最深的子视图-窗口->页面类->广告类,因为我想移动广告而不是页面。我循环遍历数组,然后检查单击的NSView(Ad)方法的子代,以查看是否是正在枚举的NSView(页面)的子代。(希望这是有意义的)。然后我把它的框架,以及

在NSLeftMouseDragged中,我将光标更改为与正在拖动的广告相似

在NSLeftMouseUp中,我查看我们希望将广告移动到哪个视图。我不知道的是如何在该视图上获取NSLeftMouseUp的NSPoint,我可以在窗口中获取它,但该点的x/y对于接收子视图来说将非常遥远…如何在子视图中检索NSPoint

...
} else if ([e type] == NSLeftMouseUp) {

    //get which view we are going to drop the ad on
    landingView = [[self contentView] hitTest:[e locationInWindow]];

    /****ui question here for Mike:
     if the mouseup event is not on a page class, should the mouse remain the dragcursor image here or should 
     we change it back to the mouse icon and stop this process****/

    if ([[landingView className] isEqual:@"Page"]) { 

        //get ad rect
        float adX = thisAdFrame.origin.x + 10.0;
        float adY = thisAdFrame.origin.y + 20.0;

        //get nspoint of mouse up event
        NSPoint p = [e locationInWindow];

        [landingView addSubview:theSubView];
        [theSubView setFrame:NSMakeRect(p.x, p.y, thisAdFrame.size.width, thisAdFrame.size.height)];

    }

}
 ...

您可能想看看NSViews
-convertPoint:fromView:
-convertPointFromBacking:
这样的方法有助于将矩形、点等转换为不同的坐标系

fromView:
指定
nil
将从该视图的窗口转换它。此代码将适用于您:

NSPoint eventLocation = [theSubView convertPoint:p fromView:nil];