3

In my swift 3 app, I have an NSSlider that I created like this:

self.myAwesomeSlider = NSSlider(frame:CGRect(x: 10, y: 100, width: 20, height: 300))
self.myAwesomeSlider?.cell = NSSlider()
self.myAwesomeSlider?.maxValue = 127
self.myAwesomeSlider?.target = self
self.myAwesomeSlider?.isContinuous = true
self.view?.addSubview(self.myAwesomeSlider!)

What I want to do now, is (from another function) update the value of this slider in realtime.

I tried to do it very basic like this:

self.myAwesomeSlider?.integerValue = Int(value)

but it doesnt update the dot. What I wish to get is new value gets calculated and dot on slider does move according to the value.

Is this possible?

thank you


Edit

self.myAwesomeSlider?.integerValue = Int(value)

Apparently, this does work, however, I have to click on another application before I see actual change.

Wesley
  • 205
  • 1
  • 2
  • 11
  • [change NSSlider Value programmatically](http://stackoverflow.com/questions/9867343/change-nsslider-value-programmatically?rq=1) Here they do it programmatically as you request :) – Kimdv Apr 25 '17 at 08:47
  • @Kimdv yes they do, and it's the normal way of setting the value, however, i'd like for mine to update in realtime, so I need to be able to set the value after the fact, which isn't exactly working for me when doing it like that. – Wesley Apr 25 '17 at 08:53
  • UI elements are only updated when program control returns to the main event loop (and must be updated on the main thread). – Martin R Apr 25 '17 at 09:00
  • @MartinR So it's not a possivbility? – Wesley Apr 25 '17 at 09:03
  • @WesleyLi http://stackoverflow.com/questions/9015767/nsslider-animation?rq=1 here is how they animate it, by changing the value. – Kimdv Apr 25 '17 at 09:38
  • @Kimdv Would you know how to implement this in Swift? Haha – Wesley Apr 25 '17 at 09:49

1 Answers1

-1
myAwesomeSlider.addTarget(self, action: "sliderValueChanged:", 
                      forControlEvents: UIControlEvents.ValueChanged)

func sliderValueChanged() {    
    var slider = sender as! UISlider
    label.text = "\(slider.value)"
}
Andy Jazz
  • 49,178
  • 17
  • 136
  • 220
ami rt
  • 935
  • 6
  • 11