滑动滑块改变view颜色

滑动滑块改变view颜色_第1张图片

import UIKit

@UIApplicationMain

class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

self.window = UIWindow(frame:UIScreen.main.bounds)

self.window?.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)

self.window?.makeKeyAndVisible()

self.window?.rootViewController = ViewController()

return true

}

}




滑动滑块改变view颜色_第2张图片
import UIKit


class ViewController: UIViewController {

// 懒加载创建三个slider

lazy var redSlider:UISlider = {

let r = UISlider(frame:CGRect(x: 20, y: 200, width: 374, height: 30))

return r

}()

lazy var greenSlider:UISlider = {

let g = UISlider(frame:CGRect(x: 20, y: 250, width: 374, height: 30))

return g

}()

lazy var blueSlider: UISlider={

let b = UISlider(frame:CGRect(x: 20, y: 300, width: 374, height: 30))

return b

}()

override func viewDidLoad() {

super.viewDidLoad()

// 控制背景颜色的slider

self.view.addSubview(redSlider)

self.view.addSubview(greenSlider)

self.view.addSubview(blueSlider)

// 设置滑块的颜色

redSlider.thumbTintColor = UIColor.red

greenSlider.thumbTintColor = UIColor.green

blueSlider.thumbTintColor = UIColor.blue

// 设置滑块的起始值

self.view.backgroundColor = UIColor.white

redSlider.value = 1.0

greenSlider.value = 1.0

blueSlider.value = 1.0

// 给这三个滑块添加关联事件

redSlider.addTarget(self, action: #selector(changeColor), for: .valueChanged)

greenSlider.addTarget(self, action: #selector(changeColor), for: .valueChanged)

blueSlider.addTarget(self, action: #selector(changeColor), for: .valueChanged)

}

func changeColor(){

self.view.backgroundColor = UIColor(red:CGFloat(redSlider.value),green:CGFloat(greenSlider.value),blue:CGFloat(blueSlider.value),alpha:1.0)

}

}

你可能感兴趣的:(滑动滑块改变view颜色)