Legolas Wang
Legolas Wang

Reputation: 2569

How to use TouchBar - Slider in Mac Catalyst?

I'm trying to add Touch Bar support for Mac Catalyst app.

And the NSSliderTouchBarItem is marked as available for catalyst, but the doubleValue of the slider is not available. Is there another way to get value from it?

Upvotes: 1

Views: 169

Answers (2)

polymerchm
polymerchm

Reputation: 228

More great uses Mhd Hejazi's Dynamic Library

    touchBarVolumeSlider = NSSliderTouchBarItem(identifier: identifier)
    touchBarVolumeSlider.label = "V"
    touchBarVolumeSlider.action = #selector(changeSliderValue)
    touchBarVolumeSlider.setValue(0.5, forKey: "doubleValue")
    Dynamic(touchBarVolumeSlider).slider.setContinuous(false)

Now the action only calls changeSliderValue when the slider stops moving. I look at here to get details on the underlying methods and properties.

Upvotes: 1

polymerchm
polymerchm

Reputation: 228

To get the position of the slider, use this:

    @objc func changeSpeed(sender: NSSliderTouchBarItem) {
        let value = sender.value(forKey: "doubleValue") as! Double
        print(value)
    }

To set its position:

    slider.setValue(0.5, forKey: "doubleValue")

The value is between 0 and 1

Upvotes: 0

Related Questions