How to prevent a NSCursor from change?

C-V*_*rel 5 macos cocoa nsview nscursor swift

I have a problem while I try to prevent a cursor from changing. I try to put an overlay view over entire window, but, if under that overlay exist an NSTextView, this will force cursor to change. I want to prevent that, and keep the arrow cursor until my overlay view will be removed.

Overriding the cursorUpdate method does not work:

override func cursorUpdate(with event: NSEvent) {
    return
}
Run Code Online (Sandbox Code Playgroud)

Thanks!

即使这个问题的答案也无济于事。此解决方案仅在叠加视图小于 TextView 时有效。

在此处输入图片说明

The*_*man 2

如果是你的,你可以通过子类化和覆盖那里NSTextView来获得你想要的行为。如果您搜索,有几个可用的示例mouseMoved:

如果不能选择对文本视图进行子类化,那么最好的办法是使用NSWindow覆盖层。

  • 使用样式蒙版创建窗口NSWindowStyleMaskBorderless
  • 使窗口不透明,设置alpha、背景颜色
  • 用于addChildWindow:ordered定位叠加层

这个答案很好地解释了这一点。

像这样的东西:

var rect = NSRect(x: self.window.frame.origin.x, y: self.window.frame.origin.y, width: self.window.contentView.frame.size.width, height: self.window.contentView.frame.size.height)
var overlay = NSWindow.init(contentRect: rect, styleMask: .borderless, backing: .buffered, defer: false)
overlay.backgroundColor = .red
overlay.isOpaque = false
overlay.alphaValue = 0.5
self.window.addChildWindow(overlay, ordered: .above)
Run Code Online (Sandbox Code Playgroud)

现在您可以将覆盖视图添加为窗口的contentView. 光标不会随着下面的视图而改变。

也可以看看。