1.创建三个滑竿(UISlider)
-
声明为全局变量
var redslider = UISlider() var greenslider = UISlider() var blueslider = UISlider()
-
初始化及设置其属性
redslider = UISlider() redslider.frame=CGRect(x: 100, y: 50, width: 200, height: 60) self.view.addSubview(redslider) redslider.maximumValue = 255 redslider.minimumValue = 0 redslider.maximumTrackTintColor = UIColor.red redslider.minimumTrackTintColor = UIColor.red redslider.addTarget(self, action: #selector(changViewColor), for: .valueChanged) greenslider = UISlider() greenslider.frame = CGRect(x: 100, y: 130, width: 200, height: 60) self.view.addSubview(greenslider) greenslider.maximumValue = 255 greenslider.minimumValue = 0 greenslider.maximumTrackTintColor = UIColor.green greenslider.minimumTrackTintColor = UIColor.green greenslider.addTarget(self, action: #selector(changViewColor), for: .valueChanged) blueslider = UISlider() blueslider.frame=CGRect(x: 100, y: 210, width: 200, height: 60) self.view.addSubview(blueslider) blueslider.maximumValue = 255 blueslider.minimumValue = 0 blueslider.maximumTrackTintColor = UIColor.blue blueslider.minimumTrackTintColor = UIColor.blue blueslider.addTarget(self, action: #selector(changViewColor), for: .valueChanged)
2. 写一个方法,实现更改视图颜色(让每一个滑竿点击事件都执行这个方法)
func changViewColor(){
let color = UIColor (colorLiteralRed: redslider.value/255.0, green: greenslider.value/255.0, blue: blueslider.value/255.0, alpha: 1.0)
self.view.backgroundColor = color
}