更新时间:2023-12-02 09:55:16
如果 NSTextView
是您的,则可以通过子类化并覆盖 mouseMoved:
来获得所需的行为。 如果您进行搜索,则有几个示例。
如果无法对文本视图进行子类化,***的方法是使用 NSWindow
NSWindowStyleMaskBorderless
样式蒙版创建窗口。 / li> 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(覆盖,有序:.above)
Now您可以将叠加视图添加为窗口的 contentView
。光标不会更改以响应以下视图。
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
}
Thanks!
Even this (question 32447739) answer does not help. This solution is valid only if the overlay view is smaller than TextView.
If the NSTextView
is yours, you can get the behaviour you want by subclassing and overriding mouseMoved:
there. There are several examples available, if you search.
If subclassing the text view is not an option, the best thing would be to use an NSWindow
for your overlay.
NSWindowStyleMaskBorderless
style mask.addChildWindow:ordered
to position your overlayThis answer explains it quite well.
Something like this:
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)
Now you can add your overlay view as the window's contentView
. The cursor will not change in response to the views below.
See also.