Skip to content

Using UISlider to retrieve a value at regular intervals (Swift5.0)

   

0, 0.5, 1.0, 1.5 ….. The following is a sample code to get the value at regular intervals of 0, 0, 5, 1, 0, 1, 1, 0, 1, 1, 5, and so on with UISlider.

alt

import UIKit
class UISliderRoundValue_20190523: 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 roundValue = roundf(sender.value * 2.0) * 0.5
// set round value
sender.value = roundValue
sliderValue.text = String(roundValue)
}
}

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