Setting interval value for UISlider
Solution 1
yourSlider.value = x;
You should really read the documentation, lots of great resources in the iOS Developer Center.
Edit: With regards to your more specific question in the comments:
yourSlider.minimumValue = -10;
yourSlider.maximumValue = 10;
[yourSlider addTarget:self action:@selector(roundValue) forControlEvents:UIControlEventValueChanged];
- (void)roundValue{
yourSlider.value = round(yourSlider.value);
}
Solution 2
I know this question is old but I just want to share how I did this.
Assuming that the UISlider object has already been initialized, first, the minimum and maximum values of the UISlider object must be set:
mySlider.minimumValue = -2.0;
mySlider.maximumValue = 2.0;
Then, set the selector that will handle the changes in the slider:
[mySlider addTarget:self action:@selector(sliderDidChangeValue:) forControlEvents:UIControlEventValueChanged];
Also, set the interval (to a property). It is important that the interval is positive. (And, really, the INTERVAL MUST BE POSITIVE.)
self.interval = 0.5 //_interval is float
Declare the method(s):
- (void)sliderDidChangeValue:(id)sender
{
UISlider *slider = (UISlider *)sender;
//Round the value to the a target interval
CGFloat roundedValue = [self roundValue:slider.value];
//Snap to the final value
[slider setValue:roundedValue animated:NO];
}
- (float)roundValue:(float)value
{
//get the remainder of value/interval
//make sure that the remainder is positive by getting its absolute value
float tempValue = fabsf(fmodf(value, _interval)); //need to import <math.h>
//if the remainder is greater than or equal to the half of the interval then return the higher interval
//otherwise, return the lower interval
if(tempValue >= (_interval / 2.0)){
return value - tempValue + _interval;
}
else{
return value - tempValue;
}
}
Solution 3
A flexible solution:
// define MIN_VALUE, MAX_VALUE and INTERVAL somewhere, such as 3, 18 and 3
slider.TouchUpInside += delegate {
touchUpInside();
};
/// <summary>
/// set value to one of intervals
/// </summary>
void touchUpInside(){
// get the nearest interval value
for(int i=MIN_VALUE; i<=MAX_VALUE; i=i+INVERVAL){
if(slider.Value > i && slider.Value < i + INVERVAL){
if(slider.Value - i < i + INVERVAL - slider.Value){
slider.Value = i;
}else{
slider.Value = i + INVERVAL;
}
break;
}
}
}
Solution 4
int sliderValue = kSliderInterval * floor((slider.value / kSliderInterval) + 0.5);
Solution 5
UISliders
do not 'increment' their value, they increase or decrease their value according to how they're touched. You can use these properties to set the slider limits:
slider.minimumValue = 0.0;
slider.maximumValue = 0.5;
Related videos on Youtube
Manmay
Updated on June 04, 2022Comments
-
Manmay almost 2 years
I need to set the interval value for an UISlider.
Please help.. -
Manmay about 13 yearsohh is that so.. I know how to set the value.. I need to set the increment interval .. Meaning if slider ranges from -10 to 10.. n I want the increment interval to be 0.5... then how do I set the interval..
-
Mike A about 13 yearsOn
UIControlEvenValueChanged
you round the slider.value. -
Manmay about 13 yearsMike, thanks dat was useful.. The slider is now incrementing with the required interval, but there is a jerk with the slider ball all time.. :(
-
Mike A about 13 yearsI'm very confused as to what it is you're trying to do exactly. If you want the slider to be smooth but only return rounded results, just use
round(yourSlider.value)
and skip all the rest. -
Muhammad Saqib over 12 yearsthankx Genc it really work for me. You just save my lot of time :)
-
izzy over 8 yearsWow, this solution simply crashes for me. terminates with uncaught exception. Think it is the event handler needs an (id) sender argument ...yup, change
- (void)roundValue{
to- (void)roundValue:(id)sender{