Search code examples
swiftxcodeuisliderboundsuiprogressview

How to limit sliding of the UISlider to a certain point?


I have a UISlider & UIProgressView to which I set a certain value. The slider handle then moves to a maximum value, as expected.

UISlider and UIProgressview Image

Now, I want to let the user slide the slider thumb to that red point. (as in the picture)

For example, I have a UISlider with a max value of 100 and min value of 0. But, I want to set the thumb image of the slider to not to cross a certain point inside a slider.

I don't want the user to slide "more left" than that progress tint but on the "right" of it, I want to let the user slide the handle back and forth up-to the red point as in the fig. OR, in other words: I want the UISlider to "stick" to the progress tint of progressView. If the progress is 80..the progress tint will display up-to 80 and the slider should start exact from that red point.

Is this sort of behavior possible in Swift?

Solution:- I simply created a view via storyboard and adjusted a image view in between the progressview and sliderview and a progress set to '1' and a width constraint for progressview with a multiplier constraints.


Solution

  • here you go.

     var prevValue : Float = 0.0
    
    @IBAction func sliderChangeValue(_ slider: UISlider) {
    
        if slider.value < prevValue {
    
            slider.value = prevValue;
        }
        else{
    
            prevValue = slider.value;
        }
    }
    

    this work fine. just tested it.