2

I'm trying to make an NSSlider's values/digits non-linear...

@IBOutlet private weak var _countSlider:NSSlider!;

let countSliderValues:[Int] = [1, 20, 50, 100, 500, 1000];
_countSlider.numberOfTickMarks = countSliderValues.count;
_countSlider.minValue = Double(countSliderValues[0]);
_countSlider.maxValue = Double(countSliderValues[countSliderValues.count - 1]);
_countSlider.allowsTickMarkValuesOnly = true;
_countSlider.integerValue = 100;

But for some reason the slider is showing linear values (1, 200, 400, 600, 800, 1000). Does anyone know why this happens?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
BadmintonCat
  • 9,416
  • 14
  • 78
  • 129
  • 2
    Because you never set the slider values, only the min and max values...? – Schemetrical May 05 '15 at 15:00
  • You know what? You are totally right! How can the NSSlider know what values to deal with?! The question is: how do I tell it the values? It's not possible in IB either (only min. and max.). I've wrote this code from an example at http://stackoverflow.com/questions/5810753/how-can-nsslider-be-customised-to-provide-a-non-linear-scale-in-cocoa – BadmintonCat May 05 '15 at 15:08

1 Answers1

3

The number of tick marks property sets tick marks that are evenly divided between the slider's min and max. There's no way to define a target value for each individual tick mark.

Your best bet is to roll your own solution by either subclassing NSSlider and NSSliderCell and coercing them to behave the way you want, or to create your own entirely novel control from scratch, specific to non-linear scaling. Nothing saying you can't still use NSSliderCell to draw the standard system parts (the track and the knob).

Joshua Nozzi
  • 60,946
  • 14
  • 140
  • 135
  • Yep: Nope. ;-) You'll have to roll your own solution that allows you to set individual values for each tick. Might be easier to build from scratch rather than trying to subclass NSSlider/NSSliderCell and bend it to your will. – Joshua Nozzi May 05 '15 at 15:16
  • 2
    @BadmintonCat its not clear if you want the scale to be log or if you want the tick marks to be positioned in log. – Schemetrical May 05 '15 at 15:16
  • 2
    @Schemetrical Good point. Also not possible to position the ticks individually but if you just want the *value*, BadmintonCat can use an `NSValueTransformer` to transform "stop number" to "scale factor". – Joshua Nozzi May 05 '15 at 15:19