当我尝试阻止光标更改时遇到问题。
我尝试在整个窗口上放置一个覆盖视图,但是,如果在该覆盖下存在一个NSTextView
,这将强制光标改变。我想防止这种情况发生,并保持箭头光标直到我的覆盖视图被删除。
重写cursorUpdate方法不起作用:
override func cursorUpdate(with event: NSEvent) {
return
}
Thanks!
Even 这个问题 https://stackoverflow.com/questions/32447739/changing-nscursor-for-nsview-above-an-nstextview答案没有帮助。仅当覆盖视图小于 TextView 时,此解决方案才有效。
If the NSTextView
是你的,你可以通过子类化和重写来获得你想要的行为mouseMoved:
there. 如果您搜索的话,有几个可用的示例 https://stackoverflow.com/questions/13658424/set-the-cursor-to-a-pointing-hand-over-a-text-view.
如果子类化文本视图不是一个选项,最好的办法是使用NSWindow
为您的叠加。
- 使用以下命令创建窗口
NSWindowStyleMaskBorderless
风格面具。
- 使窗口不透明,设置alpha、背景颜色
- Use
addChildWindow:ordered
定位您的叠加层
这个答案 https://stackoverflow.com/questions/9128134/put-a-transparent-nswindow-permanently-on-top-of-another-nswindow很好地解释了这一点。
像这样的事情:
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)
现在您可以将覆盖视图添加为窗口的contentView
。光标不会随着下面的视图而改变。
See also https://stackoverflow.com/a/49614844/233944.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)