This is the sample code to get the value with UISlider. You can take any value in between by setting a minimum and maximum value.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
class UISlider_20190522: UIViewController { | |
var sliderValue: UILabel! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
view.backgroundColor = .white | |
let initialValue: Float = 8.0 | |
let width = view.frame.width | |
let height = view.frame.height | |
let slider = UISlider() | |
slider.minimumValue = 0.0 | |
slider.maximumValue = 10.0 | |
slider.value = initialValue | |
slider.tintColor = .orange | |
slider.frame = CGRect(x: width * 0.1, y: height * 0.2, width: width * 0.8, height: height * 0.1) | |
slider.addTarget(self, action: #selector(sliderDidChangeValue(_:)), for: .valueChanged) | |
view.addSubview(slider) | |
sliderValue = UILabel() | |
sliderValue.frame = CGRect(x: width * 0.1, y: height * 0.4, width: width * 0.8, height: height * 0.1) | |
sliderValue.textAlignment = .center | |
sliderValue.text = String(initialValue) | |
view.addSubview(sliderValue) | |
} | |
@objc func sliderDidChangeValue(_ sender: UISlider) { | |
let value = sender.value | |
sliderValue.text = String(value) | |
} | |
} | |