您的位置:首页 > 产品设计 > UI/UE

UI控件之UISwitch(事件监听)

2017-08-29 11:04 495 查看
//  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 =
UIC
4000
olor.white

        }

    }

    override
func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: