这是个开关控件,当然也继承与UIControl,详细属性如下
-(void)addSwitch{
_switch = [[UISwitch alloc]initWithFrame:CGRectMake(20,200, 100, 100)];
_switch.onTintColor = [UIColor redColor];//开启时的颜色
_switch.tintColor = [UIColor greenColor];//开关的轮廓颜色
_switch.thumbTintColor = [UIColor orangeColor];//开关中间的圆圈的颜色
_switch.on = YES;
[_switch setOn:NO animated:YES];
[_switch addTarget:self action:@selector(targetAction:) forControlEvents:UIControlEventValueChanged];
[[self view]addSubview:_switch];
}
-(void)targetAction:(UISwitch*)swit{
if (swit.on) {
swit.onImage = [UIImage imageNamed:@"yongbao.png"];
NSLog(@"\n开关现在开启\n");
}else{
swit.offImage = [UIImage imageNamed:@"daku.png"];
NSLog(@"\n开关现在关闭\n");
}
}
//由于IOS7的界面风格素净不予许普通图片的出现在原生控件之上,所以,如果要加图,则只能加矢量图,要么有像素,要么无像素,因为只要有像素就会被默认绿色填充