adjusting label to slider value swift

后端 未结 6 1553
南旧
南旧 2021-01-01 14:44

I have a slider and label in my swift project. On the storyboard, I control dragged my slider onto my controller class for that page and created an outlet and also an action

相关标签:
6条回答
  • 2021-01-01 14:58

    This is what I did and it seemed to have worked:

    // For the slider
    @IBOutlet weak var slider: UISlider!
    
    // For the label displaying the slider value
    @IBOutlet weak var sliderVal: UILabel!
    
    // For the "submit" button action
    @IBAction func newSliderValue(_ sender: Any) {
        sliderVal.text = String(slider.value)
    }
    

    Note, I used a button to submit my new slider value, not what you did.

    0 讨论(0)
  • 2021-01-01 15:05

    Swift 3: Select .allEvents for its UIControlEvents.

    slider.addTarget(self, action: #selector(ViewController.updateKmsLabel(sender:)), for: .allEvents)
    // To use
    func updateKmsLabel(sender: UISlider!) {
        let value = Int(sender.value)
        DispatchQueue.main.async {
            self.kmsLabel.text = "\(value)"
            print("Slider value = \(value)")
        }
    }
    
    0 讨论(0)
  • 2021-01-01 15:09

    try this

    var currentValue = Int(slider.value)
    println("Slider changing to \(currentValue) ?")
    startTime.text = "\(currentValue) Km"
    
    0 讨论(0)
  • 2021-01-01 15:09

    If you want to do it programmatically, this is one way.

    var slider: UISlider()
    var label: UILabel()
    
    func numberValueChanged(sender: UISlider) {
        slider.setValue(Float(Int(slider.value)), animated: true)
        updateLabels(slider.value)
    }
    fun updateLabels(nV: Float?) {
        if let v = nV {
            self.label.text = "\(v)"
        }
    }
    
    slider.addTarget (self,
                     action: #selector(numberValueChanged)
                     forControlEvents: UIControlEvents.ValueChanged
    )
    

    Hope this was helpful :)

    0 讨论(0)
  • 2021-01-01 15:15

    class ViewController: UIViewController {

    @IBOutlet weak var imgView: UIImageView!
    @IBOutlet weak var slideroutlet: UISlider!
    @IBOutlet weak var lblValue: UILabel!
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
    }
    
    @IBAction func slider(_ sender: Any) {
    
        lblValue.text = String(slideroutlet.value)
        imgView.alpha = CGFloat(slideroutlet.value)
    }
    
    0 讨论(0)
  • 2021-01-01 15:17

    Update the slider value in Main queue

    @IBAction func sliderValueChanged(sender: UISlider) {
        var currentValue = Int(sender.value)
        println("Slider changing to \(currentValue) ?")
        dispatch_async(dispatch_get_main_queue(){
            sliderVal.text = "\(currentValue) Km"
        })
    }
    

    I hope this is helpful to you.

    0 讨论(0)
提交回复
热议问题