Move a Slider by steps or segments

0

I need to create a Slider that moves in steps. On a scale of 0 to 100 units, you can move in 5 steps of 20 units. I tried to do it with the Slider class that comes by default in Xcode. I had thought to round the value when I moved the Slider to the next step, but it does not work.

Currently I have the code inserted into an IBAction method that is called when I have moved the Slider like this:

int paso = 100 / 5;
[slider setValue:roundf(slider.value/paso)*paso];
    
asked by Popularfan 15.12.2016 в 10:13
source

1 answer

2

It's simple. Make a slider that goes from 0.0 to 5.0 for example. To round the steps execute the following code:

func sliderEditingDidEnd(sender: UISlider) {

        // Ajustamos con animación suave
        UIView.animate(withDuration: 0.3, animations: {
            sender.setValue(round(sender.value), animated: true)
        })

}

Afterwards, to obtain the value, you will only have to do:

let valor = slider.value * 20.0
    
answered by 15.12.2016 / 14:50
source