I am trying to implement some form of snapping or steps with the UISlider. I have written the following code but it does not work as smooth as I hoped for. It works, but when the I slide it upwards it snap 5points to the right leaving the finger not centered over the "slide-circle"
This is my code where self.lastQuestionSliderValue
is a property of the class which I have set to the initial value of the slider.
if (self.questionSlider.value > self.lastQuestionSliderValue) {
self.questionSlider.value += 5.0;
} else {
self.questionSlider.value -= 5.0;
}
self.lastQuestionSliderValue = (int)self.questionSlider.value;
The simplest solution to me was just
SWIFT VERSION
Example: You want a slider to go from 1-10000 in steps of 100. UISlider setup is as follows:
In the action func() for the slider use:
It's actually considerably easier than I first thought. Originally I was trying to get the thumbrect property and do complicated math. Here's what I ended up with:
h File:
m File:
Make sure you hook up the method and the outlet for your UISlider view and you should be good to go.
Maybe someone will need! In my situation I needed any integer step, so I used the following code:
A really simple one:
Great if you want a fast solution and you've added the target by UIControlEventValueChanged.
Another Swift approach is to do something like
You can read more about the approach and setup in my post.