UISwitch继承于UIControl
UIControl 继承于UIView
1.初始化
UISwitch *mySwitch = [[UISwitch alloc] initWithFrame:CGRectMake(100, 100, 0, 0)];
2 .设置属性
设置边框的颜色
mySwitch.tintColor = [UIColor redColor];
设置开启的颜色
mySwitch.onTintColor = [UIColor blueColor];
设置圆点的颜色
mySwitch.thumbTintColor = [UIColor greenColor];
设置开启状态 默认是NO
mySwitch.on = YES;
animated开启时候有动画效果
[mySwitch setOn:YES animated:YES];
UISwitch和UIButton一样继承UIControl 都可以触发点击方法
添加开关事件
[mySwitch addTarget:self action:@selector(mySwithOn:) forControlEvents:(UIControlEventTouchUpInside)];
3 .添加到窗口
[self.window addSubview:mySwitch];
4.释放
[mySwitch release];
实现开关事件
- (void)mySwithOn:(UISwitch *)mySwitch
{
判断是否开启
if (mySwitch.on) {
设置开启后效果
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@”提示” message:@”确定开启” delegate:self cancelButtonTitle:@”确认” otherButtonTitles:@”取消”, nil];
[alertView show];
[alertView release];
NSLog(@”开启状态”);
}else{
设置关闭效果
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@”提示” message:@”确认取消” delegate:self cancelButtonTitle:@”确认” otherButtonTitles:@”取消”, nil];
[alertView show];
[alertView release];
NSLog(@”关闭状态”);
}
}