UISlider that snaps to a fixed number of steps (like Text Size in the iOS 7 Settings app)

前端 未结 6 2194
情深已故
情深已故 2020-12-04 06:31

I\'m trying to create a UISlider that lets you choose from an array of numbers. Each slider position should be equidistant and the slider should snap to each po

6条回答
  •  有刺的猬
    2020-12-04 06:47

    If anyone also needs snaping animation then can do the following:

    1. Uncheck the continuous update of the slider from the storyboard.

    You can do the same from swift slider.isContinuous = false

    1. Add the following @IBAction in your ViewController:
       @IBAction func sliderMoved(_ slider: UISlider){
    
            let stepCount = 10
    
            let roundedCurrent = (slider.value/Float(stepCount)).rounded()
            let newValue = Int(roundedCurrent) * stepCount
    
            slider.setValue(Float(newValue), animated: true)
        }
    

    I was inspired by this answer

提交回复
热议问题