Limit sliding UISlider past certain point

前端 未结 3 1778
终归单人心
终归单人心 2021-01-19 03:06

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 sli

3条回答
  •  别那么骄傲
    2021-01-19 03:36

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

提交回复
热议问题