//
// ViewController.swift
// uiSwitch
//
// Created by hong wang on 2017/6/2.
// Copyright © 2017年 appio. 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.
//UISwitch Demo iPhone5S UISwitch默认宽度为51 高度为31,无法改变
let uiSw:UISwitch = UISwitch(frame: CGRect(x: 10, y: 50, width: 100, height: 100))
self.view.addSubview(uiSw )
//设置状态
uiSw.setOn(true, animated: true)
/*颜色设置可选项
//设置滑块颜色
uiSw.thumbTintColor = UIColor.red
//设置开状态颜色
uiSw.onTintColor = UIColor.blue
//设置关状态颜色
uiSw.tintColor = UIColor.purple
*/
//响应事件
uiSw.addTarget(self, action: #selector( ViewController.OnSw ), for: .valueChanged )
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func OnSw( sw: UISwitch){
if sw.isOn {
self.view.backgroundColor = UIColor.green
}
else{
self.view.backgroundColor = UIColor.purple
}
let cgRec = sw.frame
print(cgRec.origin.x, cgRec.origin.y, cgRec.width, cgRec.height)
}
}
// ViewController.swift
// uiSwitch
//
// Created by hong wang on 2017/6/2.
// Copyright © 2017年 appio. 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.
//UISwitch Demo iPhone5S UISwitch默认宽度为51 高度为31,无法改变
let uiSw:UISwitch = UISwitch(frame: CGRect(x: 10, y: 50, width: 100, height: 100))
self.view.addSubview(uiSw )
//设置状态
uiSw.setOn(true, animated: true)
/*颜色设置可选项
//设置滑块颜色
uiSw.thumbTintColor = UIColor.red
//设置开状态颜色
uiSw.onTintColor = UIColor.blue
//设置关状态颜色
uiSw.tintColor = UIColor.purple
*/
//响应事件
uiSw.addTarget(self, action: #selector( ViewController.OnSw ), for: .valueChanged )
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func OnSw( sw: UISwitch){
if sw.isOn {
self.view.backgroundColor = UIColor.green
}
else{
self.view.backgroundColor = UIColor.purple
}
let cgRec = sw.frame
print(cgRec.origin.x, cgRec.origin.y, cgRec.width, cgRec.height)
}
}