Interval Value for UISlider

I need to set the interval value for UISlider.

Please, help..

+8
objective-c iphone uislider
source share
6 answers

yourSlider.value = x;

You really have to read the documentation, lots of great resources at the iOS Developer Center .

Edit: Regarding 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); } 
+12
source share

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; } } } 
+5
source share

I know this question is old, but I just want to share how I did it.

Assuming that the UISlider is already initialized, the minimum and maximum values ​​of the UISlider must first 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 spacing (for the property). It is important that the interval is positive. (And, indeed, INTERVAL MUST BE POSITIVE.)

 self.interval = 0.5 //_interval is float 

Declare 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; } } 
+4
source share
 int sliderValue = kSliderInterval * floor((slider.value / kSliderInterval) + 0.5); 
+3
source share

UISliders do not "increase" their value, they increase or decrease their value depending on how they are touched. These properties can be used to set the limits of the slider:

 slider.minimumValue = 0.0; slider.maximumValue = 0.5; 
+1
source share

I would suggest that put the level just below the slider where you want to place the value and set the maximum and minimum value, then the value between the values ​​can be calculated without a dot and division and finally display the values ​​in uilabel. hope this helps

luck

0
source share

All Articles