Skip to content

Getting a value with UISlider (Swift5.0)

   

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.

alt

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

  1. Displaying an incoming call screen using CallKit.
  2. Performing UITest (E2E testing) on iOS (Swift5.0)OSでUITest(E2Eテスト)を行う(Swift5.0)
  3. Switch RootViewController with animation (Swift4.2)
  4. Parse a local Json file and display it in table view (Swift4.2)
  5. Adopting UITest with Swift(Swift4.2)
  6. Display items in a table with UITableView(Swift4.2)
  7. Screen transition in NavigationController(Swift4.2)