首页 > 代码库 > swift 实践- 11 -- UISlider
swift 实践- 11 -- UISlider
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let slider = UISlider.init(frame: CGRect.init(x: 0, y: 0, width: 300, height: 50))
slider.center = self.view.center
slider.minimumValue = http://www.mamicode.com/0
slider.maximumValue = http://www.mamicode.com/1
slider.value = http://www.mamicode.com/0.5
self.view.addSubview(slider)
// 设置滑块的值 有动画
slider.setValue(0.5, animated: true)
// 滑块值改变响应
slider.isContinuous = false // 滑块滑动停止之后才会触发 ValueChanged 事件
slider.addTarget(self, action: #selector(sliderChange(slider:)), for: .valueChanged)
// 滑块左右两边槽的颜色
slider.minimumTrackTintColor = UIColor.red
slider.maximumTrackTintColor = UIColor.green
// 滑块后面槽线两侧添加图标
slider.minimumValueImage=UIImage(named:"2") //左边图标
slider.maximumValueImage=UIImage(named:"2") //右边图标
//设置滑块的图片
slider.setThumbImage(UIImage(named:"2"),for:.normal)
}
func sliderChange(slider:UISlider){
print(slider.value)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
swift 实践- 11 -- UISlider