悬停在分隔符上时,是否可以在 NSSplitView 中暂时禁用鼠标光标更改?

Is it possible in a NSSplitView to temporarily disable the mouse cursor change when hovering on the separator?

我正在开发一个 macOS 应用程序,我有一个子类 NSSplitView,我有时会通过减少它的 alphaValue 来完全禁用它,因为子类中会发生以下情况:

override func hitTest(_ point: NSPoint) -> NSView? {
    if self.alphaValue == 1.0 {
        return super.hitTest(point)
    }    
    return nil
}

我知道这只是一个细节,但我还是想解决它。由于重写的方法,SplitView 正确地忽略了鼠标点击,但是当鼠标进入分隔视图时,光标会适时更改以反映这一点,并显示分隔移动光标。我可以暂时阻止这种情况发生吗?这是一件很小的事情,但是,在这一点上,我也很好奇。感谢您的帮助。

我找到了一种可行的方法。我以这种方式修改了 hitTest 覆盖:

override func hitTest(_ point: NSPoint) -> NSView? {
    if self.alphaValue == 1.0 {
        self.resetCursorRects()
        return super.hitTest(point)
    }
    self.discardCursorRects()
    return nil
}

不再显示光标变化。但是文档建议不要这样做:

  • (void)discardCursorRects; You need never invoke this method directly; neither is it typically invoked during the invalidation of cursor rectangles. NSWindow automatically invalidates cursor rectangles in response to invalidateCursorRectsForView: and before the view's cursor rectangles are reestablished using resetCursorRects. This method is invoked just before the view is removed from a window and when the view is deallocated.

我会等待有更多经验的人找到不违背文档的更好解决方案。谢谢

来自 resetCursorRects 的文档:

Overridden by subclasses to define their default cursor rectangles.

A subclass’s implementation must invoke addCursorRect(_:cursor:) for each cursor rectangle it wants to establish. The default implementation does nothing.

Application code should never invoke this method directly; it’s invoked automatically as described in "Responding to User Events and Actions." Use the invalidateCursorRects(for:) method instead to explicitly rebuild cursor rectangles.

防止拆分视图在禁用时定义其光标矩形:

override func resetCursorRects() {
    if self.alphaValue == 1.0 {
        super.resetCursorRects()
    }    
}

在启用和禁用拆分视图时重建光标矩形,例如:

func enable() {
    self.alphaValue = 1.0
    self.window?.invalidateCursorRects(for:self)
}

func disable() {
    self.alphaValue = 0.5
    self.window?.invalidateCursorRects(for:self)
}