使用其他答案所建议的当前应用程序或窗口事件在某种程度上可能更简单,但不是防弹的 - 可以通过编程方式停止跟踪 + 检查其他问题的相关评论。迄今为止,将滑块和滑块单元子类化更加可靠和直接,但是,在界面生成器中更新类是一个缺点:
// This is Swift 3.
import AppKit
class Slider: NSSlider
{
fileprivate(set) var tracking: Bool = false
}
class SliderCell: NSSliderCell
{
override func startTracking(at startPoint: NSPoint, in controlView: NSView) -> Bool {
(self.controlView as? Slider)?.tracking = true
return super.startTracking(at: startPoint, in: controlView)
}
override func stopTracking(last lastPoint: NSPoint, current stopPoint: NSPoint, in controlView: NSView, mouseIsUp flag: Bool) {
super.stopTracking(last: lastPoint, current: stopPoint, in: controlView, mouseIsUp: flag)
(self.controlView as? Slider)?.tracking = false
}
}