【问题标题】:How to prevent a NSCursor from change?如何防止 NSCursor 发生变化?
【发布时间】:2018-03-28 20:37:21
【问题描述】:

我在尝试阻止光标更改时遇到问题。 我尝试在整个窗口上放置一个覆盖视图,但是,如果在该覆盖下存在一个NSTextView,这将强制光标改变。我想防止这种情况发生,并保持箭头光标,直到我的覆盖视图被删除。

覆盖 cursorUpdate 方法不起作用:

override func cursorUpdate(with event: NSEvent) {
    return
}

谢谢!

即使this question 的回答也无济于事。此解决方案仅在覆盖视图小于 TextView 时有效。

【问题讨论】:

    标签: swift macos cocoa nsview nscursor


    【解决方案1】:

    如果NSTextView 是你的,你可以通过继承和覆盖mouseMoved: 来获得你想要的行为。 There are several examples available, if you search.

    如果子类化文本视图不是一个选项,最好的办法是使用 NSWindow 来覆盖。

    • 使用NSWindowStyleMaskBorderless 样式掩码创建窗口。
    • 使窗口不透明,设置 alpha,背景颜色
    • 使用addChildWindow:ordered 定位您的叠加层

    This answer 解释的很好。

    类似这样的:

    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。光标不会随着下面的视图而改变。

    见also。

    【讨论】:

      猜你喜欢
      • 2016-05-24
      • 1970-01-01
      • 2018-10-30
      • 1970-01-01
      • 1970-01-01
      • 2020-02-14
      • 2019-07-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多