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!
Just subtract the value you get from the slider from the maximum value, that will reverse the values.
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);
you can use the following formula:
let sliderValue = yourSlider.minimumValue + yourSlider.maximumValue - yourSlider.value
Will reverse your values.
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
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.
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.
Just subtract slider current value from max value.
label.text = [NSString stringWithFormat:#" %.f%% ", 100 - self.slider.value];
You can simply flip the slider horizontally using transform (Swift 5.0):
slider.transform = CGAffineTransform(scaleX: -1, y: 1);
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.
Related
I've tried to make a slider to show a percentage out of 100, but the label doesn't show that value correctly.
A UISlider has a default min and max of 0 and 1. So the slider is sending all kinds of fractional values as you move the slider such as 0.153343 and .53453545, etc. But you convert that number to an Int. That leaves you with only 0 and 1.
Either multiply the sender.value * 100 or change the slider's max value to 100.
Just set the maximumValue of the slider:
slider.maximumValue = 100
This will have the slider go between 0 and 100.
However, if you would not like to do this, try something like this:
#IBAction func valueChanged(sender: UISlider) {
let rounded = round(100 * sender.value) / 100
let final = rounded * 100
sliderLabel.text = "\(final)"
}
Made for Objective C. If anybody need.
float founded = roundf(100*self.volumeViewSlider.value)/100;
float final = founded*100;
I want to do something like this. I have a UISlider its minimum value shuld be 0 and the maximum value should be 1000.And I want to divide this range into 10 parts. Like this.
0-----100----200----300----400
When user drag the slider from 0 it should directly stop at 100 position without stopping inbetween 0 and 100 when he drag from 100 it should directly jump into 200 should not stop inbetween 100 and 200 likewise.How can I do this? please help me.
Thanks
You have to do this:
slider.maximumValue = numberOfSteps;
slider.minimumValue = 0;
slider.continuous = NO;
For setting
NSUInteger index = (NSUInteger)(slider_value + 0.5);
[slider setValue:index animated:NO];
Well, just assign your slider range 0...10 and multiply changed value by 100
I had the same need just like you the day before. With default UISlider, you can only increment either +1 or -1. Hence, if you are looking to have a increment in other values, you have to do it programmatically.
In your case, try this:
- (IBAction)[yourUISlider]:(id)sender {
[[yourUISliderProperty] setValue:((int)(( [yourUISlider].value + 50) / 100) * 100) animated:NO];
NSLog(#"%d", [yourUISlider].value );
}
It worked for me, try it out :)
And for other increment values, check out the formula here:
((int)(( [yourSlider] + [yourIncrement/2] ) / [yourIncrement]) *[yourIncrement])
Then NSLog or output the value.
I have the following code :
float temp;
temp = 2.0f;
- (IBAction)MinusTouched:(id)sender {
if (temp>0.0) {
temp=temp-0.1f;
[TempValue setText:[NSString stringWithFormat:#"%.1f",temp]];
}
}
When i perform action by clicking on the button , temp value decreases. But it prints -0.0 after 0.1 , how can i trim the negative sign ?
I mean i want an absolute float value.
For making float negative values in positive use fabs
[TempValue setText:[NSString stringWithFormat:#"%.1f",fabs(temp)]];
Try to use this method
fabs(temp)
In UISlider Control if I set "MinimumValue = 0" & "MaximimValue=2000" then I am not able to set the exact value. On release of slider thumb image it changes the value to + or - 5 to10. My slider width = 225px.
For example:- if I try to set the slider value to 100 then on release of slider thumb it shows me result like 105 or 95.
Code
IBOutlet UISlider * slider;
//"Slider" Object attached using XIB(.NIB)
slider.minimumValue = 0;
slider.maximumValue = 100;
//Attached Below Method Using XIB(.NIB) with "value Changed" event.
-(IBAction)slider_Change:(id)sender;
{
//Assigning value to UITextField
textFiled.text = [NSString stringWithFormat:#"%d",(((int) (slider.value)) * 20)];
}
Because you have set your slider range to be so large each pixel on the screen is equivalent to 8.888.
2000 / 255 = 8.8888888889
To get graduations of 1 you'll need to move the slider 1/8 of a pixel (this is impossible).
Alternatives are:
Make the slider longer
Reduce the slider range
Use a different control UIStepper
In addition to the answer by #rjstelling, it seems as if you are trying to convert the value of the slider to become an integer value. I believe that the default of the slider is a double value, this would cause the value to become rounded as it is increasing, since there will not be decimal values. Unless it is a requirement, see if you can use double.
textFiled.text = [NSString stringWithFormat:#"%f",(slider.value * 20.0)];
or if for some reason you'd want to restrict the level of decimal places you could go, you could do:
textField.text = [NSString stringWithFormat:#"%.1f", slider.value * 20.0];
This will give you the result of a decimal like 1.5 as your answer.
You could do anything with the decimal or even do something like %1.2f, %4.3f, or even %10.4f,
those will produce results like: 0.34, 0012.446, and something extreme like 000000345.1111
Hope this helps
I have a UISlider with minimumValue 0 and maximumValue. I want to make an interval of 0.5 and display it in a label (it's working).
How can I set the interval of my slider? I tried solution to round the value... but I failed.
Thank you
float RoundValue(UISlider * slider) {
return roundf(slider.value * 2.0) * 0.5;
}
when i do something like that i take an approach like the following:
#define kSTEPFRACTION .5
SliderMin = 0
sliderMax = 20
and get the value withs something like that
value = slider.value*kSTEPFRACTION;
this will give you values between 0 and 10 in steps of 0.5
sebastian