Setting interval value for UISlider

13,160

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;
Share:
13,160

Related videos on Youtube

Manmay
Author by

Manmay

Updated on June 04, 2022

Comments

  • Manmay
    Manmay almost 2 years

    I need to set the interval value for an UISlider.

    Please help..

  • Manmay
    Manmay about 13 years
    ohh 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
    Mike A about 13 years
    On UIControlEvenValueChanged you round the slider.value.
  • Manmay
    Manmay about 13 years
    Mike, 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
    Mike A about 13 years
    I'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
    Muhammad Saqib over 12 years
    thankx Genc it really work for me. You just save my lot of time :)
  • izzy
    izzy over 8 years
    Wow, 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{

Related