iOS 滑块控件 UISlider
1. UISlider 的常规使用
let slider = UISlider(frame: CGRect(x: 25, y: 750, width: 340, height: 30))
slider.maximumValue = 100
slider.minimumValue = 0
slider.value = 30
slider.minimumTrackTintColor = UIColor.green
slider.maximumTrackTintColor = UIColor.gray
slider.thumbTintColor = UIColor.red
slider.addTarget(self, action: #selector(changeSlider), for: UIControl.Event.valueChanged)
self.view.addSubview(slider)
@objc func changeSlider(slider: UISlider) {
print("UISlider value: \(slider.value)")
}
效果展示:
2. UISlider 设置进度图片
let slider = UISlider(frame: CGRect(x: 25, y: 780, width: 340, height: 30))
slider.maximumValue = 100
slider.minimumValue = 0
slider.value = 30
slider.setThumbImage(UIImage(named: "portrait"), for: UIControl.State.normal)
slider.setMinimumTrackImage(UIImage(named: "left-progress"), for: UIControl.State.normal)
slider.setMaximumTrackImage(UIImage(named: "right-progress"), for: UIControl.State.normal)
slider.addTarget(self, action: #selector(changeSlider), for: UIControl.Event.valueChanged)
slider.isContinuous = false
self.view.addSubview(slider)
效果展示:
附 Github 源码:
ViewController.swift
|