Determine when NSSlider knob is 'let go' in continuous mode

后端 未结 7 1090
青春惊慌失措
青春惊慌失措 2020-12-17 10:19

I\'m using an NSSlider control, and I\'ve configured it to use continuous mode so that I can continually update an NSTextField with the current value of the slider while the

7条回答
  •  南方客
    南方客 (楼主)
    2020-12-17 10:44

    Took me a little while to find this thread, but the accepted answer (although old) is great for detecting NSSlider state changes (slider value stopped changing being the main one I was looking for)!

    Answer in Swift (Swift 4.1):

    let slider = NSSlider(value: 1,
                          minValue: 0,
                          maxValue: 4,
                          target: self,
                          action: #selector(sliderValueChanged(sender:)))
    
    . . .
    
    @objc func sliderValueChanged(sender: Any) {
    
        guard let slider = sender as? NSSlider, 
              let event = NSApplication.shared.currentEvent else { return }
    
        switch event.type {
        case .leftMouseDown, .rightMouseDown:
            print("slider value started changing")
        case .leftMouseUp, .rightMouseUp:
            print("slider value stopped changing: \(slider.doubleValue)")
        case .leftMouseDragged, .rightMouseDragged:
            print("slider value changed: \(slider.doubleValue)")
        default:
            break
        }
    }
    

    Note: the right event types account for someone who has reversed their mouse buttons

提交回复
热议问题