滑动滑块改变view颜色

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

}

}




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)

}

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

友情链接更多精彩内容