【发布时间】:2011-12-24 15:09:14
【问题描述】:
我有一个应用程序,我在其中创建主窗口的两个子视图,这些子视图属于一个名为 Page 的类,在每个子视图上我放置一个名为 Ad 的类的另一个子视图。我在页面子视图之间拖动广告类。最初我只是重置 Ad 类视图的框架,但我真正想做的是将它放置在 NSLeftMouseUp 事件发生的位置。
我这样做的过程是在创建所有页面子视图时将它们注册到一个数组中。然后我创建了一个 NSWindow 的子类,并将该类分配给我在 IB 中的主窗口。我将页面视图数组传递给这个类。
我想覆盖 sendEvent 方法,检查事件是 NSLeftMouseDown、NSLeftMouseDragged 还是 NSLeftMouseUp。 NSLeftMouseDown 是检查并查看点击的子视图是否是窗口层次结构中最深的子视图-窗口->页面类->广告类,因为我要移动广告而不是页面。我循环遍历数组,然后检查单击的 NSView (Ad) 的 descendentOf 方法是否是被枚举的 NSView (Page) 的后代。 (希望这是有道理的)。然后我也拉它的框架。
在 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)];
}
}
...
【问题讨论】: