UISwitch 开关
#import "ViewController.h"
@interface ViewController (){
UISwitch *mySwitch;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//新建一个UISwitch开关,设置位置和大小
mySwitch = [[UISwitch alloc]initWithFrame:CGRectMake(0, 100, 100, 100)];
//设置UISwitch关闭时周围一圈的颜色,两种方法是一样的
mySwitch.tintColor = [UIColor redColor];
[mySwitch setTintColor:[UIColor redColor]];
//设置开关上的小圆点的颜色
[mySwitch setThumbTintColor:[UIColor yellowColor]];
//给开关上的小圆点添加一张图片
[mySwitch setThumbTintColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"666"]]];
//给开关添加一个方法
[mySwitch addTarget:self action:@selector(haha:) forControlEvents:UIControlEventTouchUpInside];
//将UISwitch添加到界面上
[self.view addSubview:mySwitch];
}
-(void)haha:(UISwitch *)a{
//打开则是1,关闭则是0
NSLog(@"%d", a.isOn);
//分别写出开关的开和关,就可以分支往下写代码了
if (a.isOn) {
NSLog(@"开");
}else{
NSLog(@"关");
}
}