Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limit sliding UISlider past certain point

Let's assume a have a UISlider to which I set a certain value. The slider handle then moves to that value, as expected.

Now I want to let the user slide the handle of the slider back and forth, but to not set the value of the UISlider to be less, than I assigned to it programmatically.

So for example I have a UISlider with a min value of 0 and a max value of 100 and I set its value to be 50. I don't want the user to slide "lefter" than 50 but on the "right" of it I want to let the user slide the handle back and forth.

How do you implement this sort of behaviour in Obj-C?

like image 695
Sergey Grischyov Avatar asked Apr 24 '13 12:04

Sergey Grischyov


3 Answers

You should first make sure the updates event are sent continuously:

mySlider.continuous = YES;

You can also do that in your storyboard/nib (as mentionned by @fDmitry, this is the default state in IB).

Then you have to link your slider to an IBAction in your .h file (create a link by ctrl-dragging from IB to your code), and assign it to the "Value Changed" event of your slider. This will create a method like this:

-(IBAction)sliderValueChanged:(id)sender;

Implement it that way:

- (IBAction)sliderValueChanged:(id)sender {

    float maxValue = 50.0f;

    if ([(UISlider*)sender value] > maxValue) {
        [(UISlider*)sender setValue:maxValue];
    }
}
like image 113
rdurand Avatar answered Sep 22 '22 06:09

rdurand


This one is very easy, actually :)

Create an action and assign it as selector for valueChanged action of UISlider. Inside that method, perform a simple check: If slider's value is less than 50, set it to 50.

If you need any further help, I can write some sample code after my class. Hope it helps!

like image 2
dmee3 Avatar answered Sep 22 '22 06:09

dmee3


Connect your slider to valueChanged: action. You should implement it something like:

- (void)mySliderValueChanged:(UISlider *)slider
{
  CGFloat myMinValue = 50.0f; // should be a global variable

  if(slider.value < myMinValue)
  {
    slider.value = myMinValue;
  }
}
like image 2
Dennis Pashkov Avatar answered Sep 21 '22 06:09

Dennis Pashkov