//
// ViewController.swift
// UISwitch
//
// Created by liaojianguo on 15/11/8.
// Copyright © 2015年 liaojianguo. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
var uiSwitch:UISwitch!;
override func viewDidLoad() {
super.viewDidLoad()
//uiSwitch = UISwitch()
//这个frame是没有意义的,系统的开关控件大小是确定的
uiSwitch=UISwitch(frame:CGRectMake(40,400,300,100))
//设置位置(开关大小无法设置)
uiSwitch.center=CGPointMake(100,50);
//设置默认值
uiSwitch.on=true;
uiSwitch.addTarget(self, action: Selector("switchDidChange"),
forControlEvents:UIControlEvents.ValueChanged)
self.view.addSubview(uiSwitch)
}
func switchDidChange(){
//打印当前值
print(uiSwitch.on)
if uiSwitch.on{
print("Switch is on")
//uiSwitch.setOn(true, animated:true)
}else{
print("Switch is off")
//uiSwitch.setOn(false, animated:true)
}
}
}