在NSTextView上方为NSView更改NSCursor [英] Changing NSCursor for NSView above an NSTextView
问题描述
发现了与我类似的问题(此), 但是我的问题似乎与视图层次结构更多相关.
Found a similar question to mine(this), but my issues seems to be a bit more associated with view hierarchy.
我有一个NSTextView,然后有一个其他的NSView作为同级视图.
I have a NSTextView, then as sibling views, several other NSViews on top of it.
类似于上面链接的问题,我设置了一个跟踪区域,并按如下方式应用了光标:
Similar to the question linked above, I setup a tracking area, and applied the cursor as such:
class CursorChangingView: NSView {
override func updateTrackingAreas() {
let trackingArea = NSTrackingArea(rect:
}
override func cursorUpdate(event: NSEvent) {
NSCursor.arrowCursor().set()
}
}
It does seem to work when hovering, but immediately goes back to the IBeam Cursor, which is the default cursor for NSTextViews under this CursorChangingView.
这是将鼠标悬停在某个NSView上时应用更改光标的正确方法吗?它下面的NSTextView是否会覆盖我的改写?
Is this the proper way of applying changing the cursor when hovering over a certain NSView, and is the NSTextView under it overriding my overrriding?
推荐答案
所有您需要的是子类化自定义视图,重写awakeFromNib方法,为[.mouseMoved,.activeAlways]事件添加自定义跟踪区域: NSTrackingArea信息.无需覆盖resetCursorRects
和/或updateTrackingAreas
.您所需要做的就是重写mouseMoved方法并在此处设置所需的光标:
All you need is to subclass a custom view, override awakeFromNib method, add the custom tracking area for [.mouseMoved, .activeAlways] events: NSTrackingArea Info there. There is no need to override resetCursorRects
and/or updateTrackingAreas
. All you need is to override mouseMoved method and set the desired cursor there:
关于discardCursorRects
方法的注意事项:
来自文档
您永远不需要直接调用此方法
You need never invoke this method directly
Xcode 9•Swift 4
import Cocoa
class CursorChangingView: NSView {
override func awakeFromNib() {
addTrackingArea(NSTrackingArea(rect: bounds, options: [.activeAlways, .mouseMoved], owner: self, userInfo: nil))
wantsLayer = true
layer?.backgroundColor = NSColor.cyan.cgColor
layer?.borderColor = NSColor.black.cgColor
layer?.borderWidth = 1
}
@objc override func mouseMoved(with theEvent: NSEvent) {
NSCursor.pointingHand.set()
}
}
这篇关于在NSTextView上方为NSView更改NSCursor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!