How do I reverse UISlider's min-max values?
Asked Answered
I

9

7

Is there a good way to reverse UISlider values? The default is min on the left and max on the right. I'd like it to work the opposite way.

Rotating it 180 degrees seems a bit silly. Any ideas?

Thanks!

Irizarry answered 15/2, 2013 at 1:3 Comment(0)
D
9

Just subtract the value you get from the slider from the maximum value, that will reverse the values.

Dozer answered 15/2, 2013 at 1:7 Comment(5)
Well, subtract from "max value", not "1"Antiphrasis
@borrrden, yeah, I'll update -- I was assuming the 0 to 1 default scale.Dozer
FI: if your min is 0.5 and your max is 1.5. Selected value is x and y is its reverse, then x+y=2. So, you can calculate the reverse like so: y=2−xConstitutionally
This answer gives incorrect result if your min value is non zero. See my answer below which holds true for non zero slider range.Armagh
Try this instead - yourSlider.semanticContentAttribute = .forceRightToLeft and you'll get the min-max colors right.Hezekiah
O
4

I then needed the exact same thing as you...so rotated it another 90 degrees placing it in an upside down position. Slider is symetrical, so it looks exactly the same in both positions. But the min and max values are now the opposite.

Command is...

mySlider.transform = CGAffineTransformRotate(mySlider.transform, 180.0/180*M_PI);
Overcharge answered 4/4, 2015 at 9:54 Comment(1)
That 180 / 180 is 1 and can be deleted? You don't know about radians? Need to mirror slider vertically too: slider.transform = slider.transform.scaledBy(x: 1, y: -1).Iron
L
3

you can use the following formula:

let sliderValue = yourSlider.minimumValue + yourSlider.maximumValue - yourSlider.value

Will reverse your values.

Laocoon answered 3/3, 2020 at 11:18 Comment(0)
I
2

Subclass NSSlider/UISlider. Override these two methods thus -

//Assumes minValue not necessarily 0.0

-(double)doubleValue
{

    double minVal = [self minValue];
    double maxVal = [self maxValue];

    double curValue = [super doubleValue];
    double reverseVal = maxVal - curValue + minVal;
    return reverseVal;
}

-(void)setDoubleValue:(double)aDouble
{

    double minVal = [self minValue];
    double maxVal = [self maxValue];

    double reverseVal = maxVal - aDouble + minVal;

    [super setDoubleValue:reverseVal];
}

This will invert the values allowing right/top to appear as minimum and left/bottom to appear as maximum

Interracial answered 19/9, 2014 at 0:28 Comment(0)
A
2

The accepted answer is incorrect because it assumes that the slider's Min value is 0.

In my case, my Min is 1.1 and Max is 8.0

You need to subtract the slider's value from Min+Max value to inverse the value.

Armagh answered 31/5, 2019 at 17:14 Comment(0)
H
2

Try this beautiful line -

yourSlider.semanticContentAttribute = .forceRightToLeft

This changes the min-max track colors + invert the values + you are not losing your frame like in the transform answers.

Hezekiah answered 17/7, 2019 at 16:53 Comment(0)
F
0

Just subtract slider current value from max value.

label.text = [NSString stringWithFormat:@" %.f%% ", 100 - self.slider.value];
Fondue answered 20/6, 2017 at 13:6 Comment(0)
J
0

You can simply flip the slider horizontally using transform (Swift 5.0):

slider.transform = CGAffineTransform(scaleX: -1, y: 1);
Jocelynjocelyne answered 12/4, 2019 at 2:4 Comment(0)
M
0

How about this:

slider.maximumValue = -minimumValue;
slider.minimumValue = -maximumValue;


-(void) sliderChanged:(UISlider *) slider {

      float value =  -slider.value;

      // do something with value

}

Then just use -slider.value.

Mallemuck answered 17/4, 2019 at 4:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.