Slider direction readout challenge

I was able to get the slider field to read out the motion(up, down, or static) by the adding following:

var oldSliderValue: Int = 0

@IBAction func slider(sender:NSSlider){
let value = sender.integerValue
// println("(value)")
sliderField.stringValue = sliderMovementDirection(value)
oldSliderValue = value

func sliderMovementDirection(value:Int) -> String{
if value > oldSliderValue{
return “:arrow_up:︎”
}else if value < oldSliderValue{
return “:arrow_down:︎”
}else{
return “⇔”
}
}
this works for me as long as the slider control is set to “ticks” or NOT set to continuous.
Is there a better way to get it to work that will work for all states of the slider? even a hint??
thanks
ml

The problem here is that you’re representing the slider’s position in terms of Ints rather than Doubles (or a Floats). The result is that sliderMovementDirection(_:slight_smile: will return “<->” much more often than you would like.

To represent the slider’s position in terms of Doubles, you’ll need to make a few minor changes:

(1) Change the type of oldSliderPosition to Double:

var oldSliderValue = 0.0

(2) In the implementation of the slider(_:slight_smile: action, access the slider’s doubleValue rather than its integerValue:

@IBAction func slider(sender: NSSlider) {
    let value = sender.doubleValue

(3) Change the signature of sliderMovementDirection(_:slight_smile: to accept a Double rather than an Int:

func sliderMovementDirection(value: Double) -> String

With these changes, you should see the text in the text field update as you would expect, including when the slider is continuous (continuously sends its action message as the user drags the knob) and when Only stop on tick marks is disabled. (This approach will work whether the slider has tick marks or not.)

Note that when the slider is continuous, the text field will be updated (1) when the knob is first clicked, (2) as the knob is being dragged around, and (3) when the knob is released. When you drag the knob to a particular position and then release it, the slider’s action will fire two final times: once for being dragged to the final position and a second time for being released. When the action fires both of those two final times, it will have the same doubleValue. As a result, when the slider is released, the text field will be updated to “<->”.

thanks!

Thanks for posting this help. I learned that I can get the slider.doubleValue instead of typecasting it this way:

var oldValue = Float(45.0)

oldValue = sender.floatValue

Odds and Ends

As someone mentioned in another post… I also wasted a lot of time in the documentation trying to “setNumTickMarks” instead of using slider.numTickMarks

Another red herring for me was thinking there was a Label type instead of NSText type although “Label” is a particular modification of NSText in the Xcode library pane. Also, I think Label might be an actual type in iOS land?

The “busy” challenge was where I learned the most - definitely helpful. It took me a few times to not have to think too hard what to do when creating and using new controls. I never found a solution for it on this website though, just the main chapter code. Am I looking in the right place? Actually, I am glad I didn’t find it else I would not have learned things as well.