如何循环高度的滑块值?
How to loop a Slider value for height?
我在情节提要中成功创建了一个用户身高滑块和相应的代码,用于在滑块中为每个高度级别分配一个值,但正如您在下面看到的,我必须手动将每个新的高度变量复制并粘贴到让它工作。我很好奇专家会如何简化这段代码?谢谢!
*注意 - 我删除了身高 5'2 到 6'4 的代码,以免占用太多 space。
'''
@IBOutlet weak var yourHeightEquals: UILabel!
@IBOutlet weak var heightSliderOutlet: UISlider!
@IBAction func heightSliderAction(_ sender: UISlider) {
heightSliderOutlet.value = roundf(heightSliderOutlet.value)
let yourHeightText: String = "Your Height: "
if heightSliderOutlet.value == 0 {
let yourHeightString = "Choose Your Height"
yourHeightEquals.text = yourHeightString
}
else if heightSliderOutlet.value == 1 {
let yourHeightString = "<5'0"
yourHeightEquals.text = yourHeightText + yourHeightString
}
else if heightSliderOutlet.value == 2 {
let yourHeightString = "5'0"
yourHeightEquals.text = yourHeightText + yourHeightString
}
else if heightSliderOutlet.value == 3 {
let yourHeightString = "5'1"
yourHeightEquals.text = yourHeightText + yourHeightString
}
.......
else if heightSliderOutlet.value == 19 {
let yourHeightString = "6'5"
yourHeightEquals.text = yourHeightText + yourHeightString
}
else if heightSliderOutlet.value == 20 {
let yourHeightString = ">6'5"
yourHeightEquals.text = yourHeightText + yourHeightString
}
}
'''
您可以像这样使用 switch 语句。
@IBOutlet weak var heightSliderOutlet: UISlider!
@IBOutlet weak var yourHeightEquals: UILabel!
@IBAction func heightSliderAction(_ sender: UISlider) {
let value = roundf(heightSliderOutlet.value)
switch value {
case 0:
yourHeightEquals.text = "Choose Your Height"
case 1:
yourHeightEquals.text = "Your Height : <5'0"
case 2...19:
let height = 5.0 + ((value - 2) * 0.1)
yourHeightEquals.text = "Your Height : \(height)"
default:
yourHeightEquals.text = "Your Height: >6'5"
}
}
更新 - 上面的开关代码只适用于公制系统 - 我不得不为英尺和英寸稍微修改它,仍然比我原来的代码干净得多!
@IBOutlet weak var yourHeightEquals: UILabel!
@IBOutlet weak var heightSliderOutlet: UISlider!
@IBAction func heightSliderAction(_ sender: UISlider) {
let value = roundf(heightSliderOutlet.value)
switch value {
case 0:
yourHeightEquals.text = "Choose Your Height"
case 1:
yourHeightEquals.text = "Your Height: <5'0"
case 2...11:
let height = 5.0 + ((value - 2) * 0.1)
let heightstring = String(height)
let heightfoot = heightstring.dropLast(2)
let heightinch = heightstring.dropFirst(2)
let heightfootinch = heightfoot + "'" + heightinch
yourHeightEquals.text = "Your Height: \(heightfootinch)"
case 12:
yourHeightEquals.text = "Your Height: 5'10"
case 13:
yourHeightEquals.text = "Your Height: 5'11"
case 14...19:
let height = 6.0 + ((value - 14) * 0.1)
let heightstring = String(height)
let heightfoot = heightstring.dropLast(2)
let heightinch = heightstring.dropFirst(2)
let heightfootinch = heightfoot + "'" + heightinch
yourHeightEquals.text = "Your Height: \(heightfootinch)"
default:
yourHeightEquals.text = "Your Height: >6'5"
}
}
我在情节提要中成功创建了一个用户身高滑块和相应的代码,用于在滑块中为每个高度级别分配一个值,但正如您在下面看到的,我必须手动将每个新的高度变量复制并粘贴到让它工作。我很好奇专家会如何简化这段代码?谢谢!
*注意 - 我删除了身高 5'2 到 6'4 的代码,以免占用太多 space。
'''
@IBOutlet weak var yourHeightEquals: UILabel!
@IBOutlet weak var heightSliderOutlet: UISlider!
@IBAction func heightSliderAction(_ sender: UISlider) {
heightSliderOutlet.value = roundf(heightSliderOutlet.value)
let yourHeightText: String = "Your Height: "
if heightSliderOutlet.value == 0 {
let yourHeightString = "Choose Your Height"
yourHeightEquals.text = yourHeightString
}
else if heightSliderOutlet.value == 1 {
let yourHeightString = "<5'0"
yourHeightEquals.text = yourHeightText + yourHeightString
}
else if heightSliderOutlet.value == 2 {
let yourHeightString = "5'0"
yourHeightEquals.text = yourHeightText + yourHeightString
}
else if heightSliderOutlet.value == 3 {
let yourHeightString = "5'1"
yourHeightEquals.text = yourHeightText + yourHeightString
}
.......
else if heightSliderOutlet.value == 19 {
let yourHeightString = "6'5"
yourHeightEquals.text = yourHeightText + yourHeightString
}
else if heightSliderOutlet.value == 20 {
let yourHeightString = ">6'5"
yourHeightEquals.text = yourHeightText + yourHeightString
}
}
'''
您可以像这样使用 switch 语句。
@IBOutlet weak var heightSliderOutlet: UISlider!
@IBOutlet weak var yourHeightEquals: UILabel!
@IBAction func heightSliderAction(_ sender: UISlider) {
let value = roundf(heightSliderOutlet.value)
switch value {
case 0:
yourHeightEquals.text = "Choose Your Height"
case 1:
yourHeightEquals.text = "Your Height : <5'0"
case 2...19:
let height = 5.0 + ((value - 2) * 0.1)
yourHeightEquals.text = "Your Height : \(height)"
default:
yourHeightEquals.text = "Your Height: >6'5"
}
}
更新 - 上面的开关代码只适用于公制系统 - 我不得不为英尺和英寸稍微修改它,仍然比我原来的代码干净得多!
@IBOutlet weak var yourHeightEquals: UILabel!
@IBOutlet weak var heightSliderOutlet: UISlider!
@IBAction func heightSliderAction(_ sender: UISlider) {
let value = roundf(heightSliderOutlet.value)
switch value {
case 0:
yourHeightEquals.text = "Choose Your Height"
case 1:
yourHeightEquals.text = "Your Height: <5'0"
case 2...11:
let height = 5.0 + ((value - 2) * 0.1)
let heightstring = String(height)
let heightfoot = heightstring.dropLast(2)
let heightinch = heightstring.dropFirst(2)
let heightfootinch = heightfoot + "'" + heightinch
yourHeightEquals.text = "Your Height: \(heightfootinch)"
case 12:
yourHeightEquals.text = "Your Height: 5'10"
case 13:
yourHeightEquals.text = "Your Height: 5'11"
case 14...19:
let height = 6.0 + ((value - 14) * 0.1)
let heightstring = String(height)
let heightfoot = heightstring.dropLast(2)
let heightinch = heightstring.dropFirst(2)
let heightfootinch = heightfoot + "'" + heightinch
yourHeightEquals.text = "Your Height: \(heightfootinch)"
default:
yourHeightEquals.text = "Your Height: >6'5"
}
}