我如何制作具有步长值的 UISlider

How can i make a UISlider with step value

希望有人能帮助我。我试图在 Swift

中制作一个步长值从 1 到 5 的 UISlider

我的代码是:

@IBAction func yearChange(sender: UISlider) {

    let step: Float = 1
    let currentValue = round(sender.value / step) * step
    print("\(currentValue) year")

}

希望对您有所帮助

let slider = UISlider()
let total: Float = 5
slider.maximumValue = total
slider.minimumValue = 1

@IBAction func yearChange(sender: UISlider) {
    
    let step: Float = 1
    let currentValue = round((sender.value - sender.minimumValue) / step)
    print("\(currentValue) year")
    
}

我用这段代码解决了这个问题:

let step: Float = 1
let roundedValue = round(sender.value / step) * step
sender.value = roundedValue