版权声明:本文为延成原创文章,转载请标明出处
@interface UISwitchViewController ()
{
UISwitch* _cSwitch;
}
@end
-(void)createUISwitch{
_cSwitch = [[UISwitch alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
[_cSwitch setOn:YES animated:YES];
[_cSwitch setOnTintColor:[UIColor redColor]];//开启时颜色
[_cSwitch setThumbTintColor:[UIColor greenColor]];//按钮颜色
[_cSwitch addTarget:self action:@selector(pressSwitch:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_cSwitch];
}
-(void)pressSwitch:(UISwitch*)swit{
if (swit.on == YES) {
NSLog(@"打开开关");
}else{
NSLog(@"关闭开关");
}
}