我很难使用NSTrackingArea
更新光标来显示我的自定义可拖动对象的NSView
大小。这是一个例子:
NSRect
用于手柄:
var handleBottomLeft: NSRect {
NSRect(x: self.bounds.origin.x,
y: self.bounds.origin.y,
width: handleMargin + borderWidth,
height: handleMargin + borderWidth)
}
追踪区域:
var bottomLeftTrackingArea: NSTrackingArea?
override func updateTrackingAreas() {
if let area = bottomLeftTrackingArea {
self.removeTrackingArea(area)
}
bottomLeftTrackingArea = nil
bottomLeftTrackingArea = NSTrackingArea(rect: handleBottomLeft, options: [.activeInKeyWindow, .cursorUpdate], owner: self, userInfo: nil)
addTrackingArea(bottomLeftTrackingArea!)
super.updateTrackingAreas()
}
最后,游标更新:
override func cursorUpdate(with event: NSEvent) {
switch event.trackingArea {
case bottomLeftTrackingArea:
print("pointing hand")
NSCursor.pointingHand.set()
default:
print("arrow")
super.cursorUpdate(with: event)
}
}
光标在进入或离开蓝色框时应该会发生变化,但只有 50-75% 的时间会发生这种情况。这是一个光标在进入时不改变但在离开时改变的示例。