【问题标题】:How to loop a Slider value for height?如何循环高度的滑块值?
【发布时间】:2021-06-08 00:55:30
【问题描述】:

我成功地在情节提要中为用户高度创建了一个滑块,并在相应的代码中为每个高度级别的滑块分配了一个值,但是如下所示,我必须手动复制并粘贴每个新的高度变量才能获得它去工作。我很好奇专家将如何简化此代码?谢谢!

*注意 - 我删除了高度 5'2 到 6'4 的代码,以免占用太多空间。

'''

@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
    }
    
}

'''

【问题讨论】:

  • 我需要你们所有的代码,否则我不知道表演的规则

标签: ios swift xcode loops slider


【解决方案1】:

你可以像这样使用 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"
    }
}

【讨论】:

  • 我没有在 Xcode 中运行代码,所以在运行代码之前将“值”转换为整数类型。谢谢你。
  • 这很棒!刚刚为我节省了大约 150 行代码:D
  • 更新 - 上面的代码只适用于公制 - 我不得不稍微修改一下英尺和英寸,仍然比我的原始代码干净得多!
【解决方案2】:

更新 - 上面的开关代码只适用于公制 - 我不得不稍微修改一下英尺和英寸,仍然比我的原始代码更干净!

@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"
    }
    
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多