Search code examples
macoscocoaswiftnsslider

NSSlider with non-linear scale


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?


Solution

  • 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).