UI控件之UISwitch(事件监听)

//  UISwitch

//

//  Created by Catherine on 2017/8/29.

//  Copyright © 2017 Catherine. All rights reserved.

//


import UIKit


class ViewController: UIViewController {


    override func viewDidLoad() {

        super.viewDidLoad()

        // Do any additional setup after loading the view, typically from a nib.

        //大小是默认的因此只有前两个位置参数有作用

        let myswitch : UISwitch = UISwitch(frame: CGRect(x: 100, y: 100, width: 100, height: 100))

        //进行属性自定义设置

        //设置滑块的颜色

        myswitch.thumbTintColor = UIColor.red

        //设置开关开启的颜色

        myswitch.onTintColor = UIColor.blue

        //设置关闭开关的渲染颜色(边框的颜色、切换--即点击时的背景色)

        myswitch.tintColor = UIColor.purple

        //添加事件

        myswitch.addTarget(self, action: #selector(switchClick(swi:)), for:UIControlEvents.valueChanged )//检测有值的变化

        self.view.addSubview(myswitch)

        

    }

    func switchClick(swi:UISwitch){

        if swi.isOn{

            self.view.backgroundColor = UIColor.gray

        }else{

            self.view.backgroundColor = UIColor.white

        }

    }

    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }



}


猜你喜欢

转载自blog.csdn.net/catherine981234/article/details/77675533