gpt4 book ai didi

cocoa - 在 NSWindow 中获取 mouseUp 上 subview 的 NSPoint

转载 作者:行者123 更新时间:2023-12-03 16:38:31 25 4
gpt4 key购买 nike

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

我执行此操作的过程是在创建所有页面 subview 时将其注册到一个数组中。然后我创建了 NSWindow 的子类,并将该类分配给 IB 中的主窗口。我将页面 View 数组传递给此类。

我想重写sendEvent方法,检查事件是否是NSLeftMouseDown、NSLeftMouseDragged或NSLeftMouseUp。 NSLeftMouseDown 是检查单击的 subview 是否是窗口层次结构的最深 subview - 窗口->页面类->广告类,因为我想移动广告而不是页面。我循环遍历该数组,然后检查单击的 NSView (Ad) 的 DescendentOf 方法,以查看是否是正在枚举的 NSView (Page) 的后代。 (希望这是有道理的)。然后我也拉动它的框架。

在 NSLeftMouseDragged 中,我将光标更改为类似于被拖动的广告。

在 NSLeftMouseUp 中,我检查我们要将广告移动到哪个 View 。我不明白的是如何在该 View 上获取 NSLeftMouseUp 的 NSPoint,我可以为窗口获取它,但该点的 x/y 对于接收 subview 来说将很遥远......我如何检索 subview 内的 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 将从该 View 的窗口转换它。此代码将适用于您:

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

关于cocoa - 在 NSWindow 中获取 mouseUp 上 subview 的 NSPoint,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8053911/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com