UISwitch常用属性

isON – 是否是打开状态
onTintColor – 开启状态颜色
tintColor – 关闭状态颜色
thumbTintColor – 圆形按钮颜色
onImage – 开启状态图片
offImage – 关闭状态图片
常用方法

– (void)setOn:(BOOL)on animated:(BOOL)animated;// does not send action

//设置开关状态并带有动画效果

小Demo

#import “ViewController.h”

@interface ViewController ()

@property (nonatomic,strong) UISwitch *mySwitch;//switch是关键字,不能用啊

@property (nonatomic,strong) UILabel *myLabel;

@end

@implementation ViewController

– (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

_myLabel = [[UILabel alloc]initWithFrame:CGRectMake(50, 50, 200, 40)];

_myLabel.text = @”变色龙”;

[self.view addSubview:_myLabel];

_mySwitch = [[UISwitch alloc]init];

_mySwitch.frame = CGRectMake(50, 100, 100, 100);

//_mySwitch.backgroundColor = [UIColor redColor];

[self.view addSubview:_mySwitch];

//设置开关开启状态颜色

_mySwitch.onTintColor = [UIColor greenColor];

//设置开关关闭状态颜色

_mySwitch.tintColor = [UIColor redColor];

//设置圆形按钮颜色

// _mySwitch.thumbTintColor = [UIColor purpleColor];

//    //设置开启状态图片

//    _mySwitch.onImage = [UIImage imageNamed:@””];

//    //设置关闭状态图片

//    _mySwitch.offImage = [UIImage imageNamed:@””];

[self switchAction];

[_mySwitch addTarget:self action:@selector(switchAction) forControlEvents:UIControlEventValueChanged];

//设置开关状态并带有动画效果

[_mySwitch setOn:YES animated:YES];

}

-(void)switchAction{

if (_mySwitch.isOn) {

NSLog(@”开关开启状态”);

_myLabel.textColor = [UIColor greenColor];

}else {

NSLog(@”开关关闭状态”);

_myLabel.textColor = [UIColor redColor];

}

}

– (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

@end

分享到: 更多
Separator image Posted in IOS.