Search code examples
iosswiftuikituislider

Animate changing a slider's maximum value


I am trying to animate a slider when setting the new maximum value. I set the maximum value, as such:

You may be confused to why there is a .slider property - it's my own custom class.

fingerSizeSlider.slider.maximumValue = Float(newValue) + 0.99

I have tried using UIView.animate which did not work, and I cannot do the following:

fingerSizeSlider.slider.setValue(fingerSizeSlider.slider.value, animated: true)

The slider thumb automatically moves (which is good) but it does not animate, and the line of code directly above has no effect (because obviously, I am setting to the same value).

How can I animate this maximumValue property of the slider?

Here is a video to illustrate the issue (a gif is too choppy).


Solution

  • A slider draws itself using private subviews. You need to animate the layout of those subviews.

    UIView.animateWithDuration(0.2) {
       fingerSizeSlider.slider.maximumValue = Float(newValue) + 0.99
       fingerSizeSlider.slider.layoutIfNeeded() 
    }