import UIKit
class ViewController: UIViewController {
//懒加载创建label
lazy var label:UILabel = {
let l = UILabel(frame:CGRect(x: 20, y: 50, width: 374, height: 50))
l.textAlignment = .center
return l
}()
//懒加载创建slider
lazy var slider:UISlider = {
let s = UISlider(frame:CGRect(x: 20, y: 120, width: 374, height: 30))
return s
}()
//懒加载创建三个slider
lazy var red:UISlider = {
let r = UISlider(frame:CGRect(x: 20, y: 200, width: 374, height: 30))
return r
}()
lazy var green:UISlider = {
let g = UISlider(frame:CGRect(x: 20, y: 250, width: 374, height: 30))
return g
}()
lazy var blue:UISlider = {
let b = UISlider(frame:CGRect(x: 20, y: 300, width: 374, height: 30))
return b
}()
override func viewDidLoad() {
super.viewDidLoad()
//把label添加到父视图
self.view.backgroundColor = #colorLiteral(red: 0.9098039269, green: 0.4784313738, blue: 0.6431372762, alpha: 1)
self.view.addSubview(label)
label.text = "中华小当家"
//把slider添加到父视图上
self.view.addSubview(slider)
slider.minimumValue = 10.0
slider.maximumValue = 50.0
slider.addTarget(self, action: #selector(changeFontSize), for: .valueChanged)
//控制背景颜色的三个slider
self.view.addSubview(red)
self.view.addSubview(green)
self.view.addSubview(blue)
//设置滑块的颜色
red.thumbTintColor = #colorLiteral(red: 0.9254902005, green: 0.2352941185, blue: 0.1019607857, alpha: 1)
green.thumbTintColor = #colorLiteral(red: 0.4666666687, green: 0.7647058964, blue: 0.2666666806, alpha: 1)
blue.thumbTintColor = #colorLiteral(red: 0.2588235438, green: 0.7568627596, blue: 0.9686274529, alpha: 1)
// 设置滑块的起始值
self.view.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
red.value = 1.0
green.value = 1.0
blue.value = 1.0
//给这三个滑块添加关联事件
red.addTarget(self, action: #selector(changeColor), for: .valueChanged)
green.addTarget(self, action: #selector(changeColor), for: .valueChanged)
blue.addTarget(self, action: #selector(changeColor), for: .valueChanged)
}
func changeFontSize(sender:UISlider){
label.font = UIFont.boldSystemFont(ofSize: CGFloat(sender.value))
}
func changeColor(sender:UISlider){
self.view.backgroundColor = UIColor(red: CGFloat(red.value), green: CGFloat(green.value), blue: CGFloat(blue.value), alpha: 1.0)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}