希望有人可以帮助我。我试图在Swift中制作一个步长值为1到5的UISlider
我的代码是:
@IBAction func yearChange(sender: UISlider) {
let step: Float = 1
let currentValue = round(sender.value / step) * step
print("\(currentValue) year")
}
答案 0 :(得分:1)
我找到了解决方案
let step: Float = 1
let roundedValue = round(sender.value / step) * step
sender.value = roundedValue
print("\(Int(roundedValue))")
答案 1 :(得分:0)
希望这个帮助
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")
}
答案 2 :(得分:0)
我用这段代码解决了这个问题:
let step: Float = 1
let roundedValue = round(sender.value / step) * step
sender.value = roundedValue