Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting interval value for UISlider

I need to set the interval value for an UISlider.

Please help..

like image 767
Manmay Avatar asked Apr 11 '11 06:04

Manmay


2 Answers

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);
}
like image 147
Mike A Avatar answered Nov 03 '22 02:11

Mike A


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;
            }
        }           
    }
like image 25
Genc Avatar answered Nov 03 '22 02:11

Genc