【问题标题】:Find the top-most NSView object located under NSPoint position查找位于 NSPoint 位置下的最顶层 NSView 对象
【发布时间】:2023-04-03 19:13:02
【问题描述】:

我知道这个问题已经在这里以几种形式提出过,但不幸的是,没有一个建议的解决方案对我有用。

我正在实施 多点触控我的 osx 应用程序的 em> 功能。
我的问题:
我需要解析屏幕上的位置,由屏幕中的 NSPoint 描述坐标,到驻留在此位置(如果有)下的 我的应用程序 的最顶部 NSView 对象。

这有点像 Windows OS 下的 HWND WindowFromPoint( Point)

需要考虑的几点:

  • 我的应用程序管理多个NSWindows,其中每个窗口包含多个NSViews 的层次结构,
  • 我感兴趣的NSView 不一定是应用程序Key WindowMain Window,因为它可以是我的任何其他当前非活动的NSView 对象,恰好位于此屏幕位置下,
  • 在一个特定的NSPoint 下,我可能会有多个NSWindows 和/或NSViews。在这种情况下,我只会对最顶层的NSView 感兴趣,
  • NSView A 可能位于 NSView B 之上,部分隐藏它,但 NSPoint 位置仅存在于 B 上,它不是应用程序的最顶层窗口(但只有这个位置的最顶层)。在这里我再次对 B 感兴趣。

我设法做的事情:

  • 为其所有窗口枚举NSApp (NSApp.windows),
  • 枚举 NSWindow 的视图 (NSWindow.contentView.subviews),
  • 为其子视图枚举NSView (NSView.subviews)

这样做我设法枚举了我的应用程序的所有NSViews,但我仍然需要过滤掉不相关的不可见的NSViews。

没有对我有用的事情:

  • NSView.hitTest 返回nil 以获得有效位置,
  • NSView.layer.zPosition alwyas 始终为零 (0),
  • NSView.subviews 列表的顺序也不反映当前的 GUI 布局,
  • 测试NSView 是否可见也无济于事,因为如果此窗口被其他窗口隐藏,它也会返回 true。

我的环境:
Mac,OSX El-Capitan,XCode-7,Cocoa,Objective-C

感谢您的帮助!
PazO

【问题讨论】:

  • 您将hitTest发送到哪个视图?
  • 对每个NSView 枚举。即 - 我的应用程序 afaik 中的所有 NSView 对象。都将返回nil...
  • 调用hitTest:window.contentView 应该足够了。您是否将点转换为窗口坐标?

标签: objective-c macos cocoa xcode7 osx-elcapitan


【解决方案1】:

找到了:

// 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 进行测试,但为了代码简洁,我将其省略。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-02
    • 2013-07-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多