Search code examples
uislider

how to set the break point for UISlider?


I have two UISlider called slider1

slider1.minimumValue = 0; slider1.maximumValue = 100;

i want to set a break point,such as 60, if the slider1 move to 60 from 0(left) to 60(right), it will stop here the thumb can not move to right, but it can move to left. so how can i do?

please take a look the following code, it doesn't work, thanks

-(IBAction)s1valuechanged:(id)sender{
if ((int)slider1.value > 60) {
    slider1.userInteractionEnabled = FALSE;
}
else{
    slider1.userInteractionEnabled =TRUE;
}
}

Solution

  • I'm having a bit of trouble understanding your question, so I'm going to assume that your slider looks like the following, and you want to prevent the user from moving the slider to a value greater than 60:

    0 ----------60------100
    |---valid----|-invalid-|

    All you would need is the following:

    -(IBAction)s1valuechanged:(id)sender{
        if ((int)slider1.value > 60) {
            slider.value = 60;
        }
    }
    

    In other words, whenever the user tries to move the slider to a value greater than 60, move the slider back to 60.