2

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.

Aashish
  • 2,532
  • 2
  • 23
  • 28

2 Answers2

3

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.

junaidsidhu
  • 3,539
  • 1
  • 27
  • 49
  • I noticed a flaw in the logic, prevValue will equal to slider.value if slider.value is larger or equal to prevValue. Wouldn't that make a one way movement for the slider? - I edited it to make it relevant to @Aashish – Ben Ong Dec 15 '16 at 09:34
  • he don't want the user to slide "lefter" than that progress. it worked fine. as equal will be the same as slider has. – junaidsidhu Dec 15 '16 at 09:42
  • In case you did not notice, that logic will prevent any bit of movement left with or without progress value, every time the value increases over the prevValue, prevValue is updated to that of the latest value and value will to adjusted to equal to that of prevValue if user attempts to decrease it down below prevValue. That is a one way slider, @Aashish wants that slider to move freely between the progress value and maximum value, so naturally you have to permit certain movement leftwards until it equals to that of the progress value. – Ben Ong Dec 15 '16 at 09:48
  • its a general solution for him and will work fine for him. he can adopt the logic and update it as per the requirement. – junaidsidhu Dec 15 '16 at 09:51
  • I want to let the user slide the slider thumb to that point only. What should I do to do that? – Aashish Dec 15 '16 at 10:02
  • @Aashish for that you just change the less than (<) to greater them (>), user brain boy... – junaidsidhu Dec 15 '16 at 10:15
  • @JayiODroid I updated the question, if it eases you.. ;) – Aashish Dec 16 '16 at 06:35
  • I am not sure why its getting so tough for you, its too simple – junaidsidhu Dec 16 '16 at 07:21
  • I'm asking about the UI of slider and you are talking about the value for slider. We're so confused I guess.. – Aashish Dec 16 '16 at 08:15
1

You can set the slider to sent on action on value change and in that action set the value of the slider to be equal to the value of 'progress' if it went lower.

Ben Ong
  • 913
  • 1
  • 10
  • 25
  • Yeah @Ben I did that, so the minimum value of slider is equal to the progressed. But, I want the slider to be not able to move behind the _progress_ . Sorry if I wasn't clear before. I will edit my question. – Aashish Dec 15 '16 at 09:17
  • I meant the value not minimum of the slider, and it would seem @Jay gave my answer in codes, you can refer to it if it is clearer for you – Ben Ong Dec 15 '16 at 09:32