How to prevent a NSCursor from change?

我的未来我决定 提交于 2019-12-10 16:56:50

问题


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.


回答1:


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.

  • Create the window with the NSWindowStyleMaskBorderless style mask.
  • Make the window opaque, set the alpha, background color
  • Use addChildWindow:ordered to position your overlay

This 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.



来源:https://stackoverflow.com/questions/49543750/how-to-prevent-a-nscursor-from-change

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!