Limit sliding UISlider past certain point

不想你离开。 提交于 2019-12-01 19:42:01

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];
    }
}

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!

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;
  }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!