我知道这个问题在这里已经以几种形式提出了,但不幸的是,没有建议的解决办法对我有用。
我正在为我的osx应用程序实现多点触摸功能。
我的问题:
我需要将屏幕上由NSPoint以屏幕坐标描述的位置解析为位于此位置下(如果有的话)的应用程序NSView 中的最顶层的NSView对象。
这有点像HWND WindowFromPoint( Point)操作系统下的Windows。
有几点需要考虑:
NSWindow,其中每个窗口包含几个NSView的层次结构,NSView不一定是应用程序Key Window或Main Window,因为它可能是我目前正在此屏幕位置下的其他非活动NSView对象中的任何一个,NSPoint下,我可能会有一个以上的NSWindows和/或NSViews。在这种情况下,我只会对最顶层的NSView感兴趣,NSView A可能在NSView B的顶部,部分地隐藏了它,但是NSPoint位置只存在于B上,这不是应用程序最顶层的窗口(而是这个位置的最上面的窗口)。在这里,我将再次对B感兴趣。我设法做的事情:
NSApp的所有窗口(NSApp.windows),NSWindow的视图(NSWindow.contentView.subviews),NSView的子视图(NSView.subviews)为此,我成功地枚举了我的应用程序的所有NSView,但我仍然需要过滤掉不可见的无关NSView。
不适合我的东西:
NSView.hitTest返回有效位置的nil,NSView.layer.zPosition alwyas为0 (0),NSView.subviews列表的顺序也不反映当前的GUI布局,NSView是否是可见的也没有帮助,因为如果这个窗口被其他窗口隐藏,它也会返回true。我的环境:
Mac,OSX El-Capitan,XCode-7,Cocoa,Objective-C
谢谢你的帮助!
PazO
发布于 2016-10-17 16:19:21
找到它:
// 1. Get the Window-Number of the NSWindow object owning this point:
NSInteger wndNumber = [NSWindow windowNumberAtPoint:(point) belowWindowWithWindowNumber:(0)];
// 2. Get the NSWindow object associated with this particular Window-Number:
NSWindow* window = [NSApp windowWithWindowNumber:(wndNumber)];
// 3. Convert NSPoint values from System-coordinates to NSWindow-coordinates (Thanks @Willeke for his comment)
NSRect rctScreen;
rctScreen.origin = point;
rctScreen.size.height = rctScreen.size.width = 0;
NSRect rctWindow = [window convertRectFromScreen:rctScreen];
// 4. Now do the hitTest:
NSView* viewFound = [[window contentView] hitTest:rctWindow.origin];显然,每一行都应该测试nil,但为了代码简洁起见,我忽略了这一点。
https://stackoverflow.com/questions/40027652
复制相似问题