三個滑竿代表紅綠藍,修改view的顏色

weixin_33797791發表於2016-11-24

  var slider1 :UISlider! = nil

var slider2 :UISlider! = nil

var slider3 :UISlider! = nil


view.backgroundColor = #colorLiteral(red: 0.4745098054, green: 0.8392156959, blue: 0.9764705896, alpha: 1)

slider1 = UISlider()

slider1.frame = CGRect(x: 20, y: 50, width: 354, height: 40)

view.addSubview(slider1)

slider1.value = 0.5

slider1.thumbTintColor = UIColor.brown

slider1.minimumValue = 0

slider1.maximumValue = 255

slider1.addTarget(self , action: #selector(siderAction), for:.valueChanged)

slider2 = UISlider()

slider2.frame = CGRect(x: 20, y: 100, width: 354, height: 40)

view.addSubview(slider2)

slider2.thumbTintColor = UIColor.red

slider2.minimumValue = 0

slider2.maximumValue = 255

slider2.minimumTrackTintColor = UIColor.green

slider2.maximumTrackTintColor = UIColor.red

slider2.addTarget(self , action: #selector(siderAction), for:.valueChanged)

slider3 = UISlider()

slider3.frame = CGRect(x: 20, y: 150, width: 354, height: 40)

view.addSubview(slider3)

slider3.thumbTintColor = UIColor.yellow

slider3.minimumValue = 0

slider3.maximumValue = 255

slider3.addTarget(self , action: #selector(siderAction), for:.valueChanged)

}

func siderAction(slider:UISlider){

let color = UIColor (colorLiteralRed: slider1.value/255, green: slider2.value/255, blue: slider3.value/255, alpha: 1.0)

self.view.backgroundColor = color

}

相關文章