Search code examples
arraysswiftuislider

Swift UISlider that gets its values from an array of numbers


I'm a newbie to swift and believe me I've searched and searched for an answer already. I want to create UISliders that get their values from an array of numbers in Swift. Its a camera app so the example array should be obvious.

@IBAction func isoValueChanged(sender: UISlider) {
    let isoArray = ["24", "32", "50","64","80","100","125","160","200","250","320","400","500","640","720","800","1000","1250","1600","1800"] // available iPhone 6s ISO settings I believe
    let currentValue = // What do I need?
    isoText.text = "\(currentValue)"
}

Even harder would be representing shutter speeds from 1/1000 - 32! From what I see out there this is not an easy one because there is no mathematical representation to calc from the array. Is it even possible?


Solution

  • I'm not quite sure I understand what you want this for but I'm guessing this is right.

    // Global Variable or something like this (accessible from multiple functions)
    let isoArray = ["24", "32", "50","64","80","100","125","160","200","250","320","400","500","640","720","800","1000","1250","1600","1800"] // available iPhone 6s ISO settings I believe
    
    func functionThatCreatesTheSliderYo(...) {
        slider.minimumValue = 0
        slider.maximumValue = isoArray.count
        slider.continuous = false
    }
    
    @IBAction func isoValueChanged(sender: UISlider) {
        // instead of Int(val) you may want to round(val) for a better UI
        let currentValue = isoArray[Int(sender.value)]
        isoText.text = "\(currentValue)"
    }