UISwitch是左右滑动关闭,或开启的开机按钮视图控件
UISwitch *switchView = [[UISwitch alloc] init];
// 添加到父视图
[self.view addSubview:switchView];
// 改变原点坐标,通常情况只设置其原点坐标,大小为默认(frame = (0 0; 51 31))
CGRect rect = switchView.frame;
rect.origin.x = 50.0;
rect.origin.y = 50.0;
switchView.frame = rect;
NSLog(@"switchView %@", switchView);
// 其他属性设置
// 设置开启后的背景颜色
switchView.onTintColor = [UIColor redColor];
// 设置关闭后的边框颜色
switchView.tintColor = [UIColor orangeColor];
// 设置拨弄开关的颜色
switchView.thumbTintColor = [UIColor purpleColor];
// 设置打开状态的图标-无效??
switchView.onImage = [UIImage imageNamed:@"openImage"];
// 设置关闭状态的图标-无效??
switchView.offImage = [UIImage imageNamed:@"closeImage"];
// 设置开关状态,默认是关闭,即NO
switchView.on = NO;
// 添加响应方法
[switchView addTarget:self action:@selector(showLable:) forControlEvents:UIControlEventValueChanged];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50.0, 100.0, (CGRectGetWidth(self.view.bounds) - 50.0 * 2), 40.0)];
[self.view addSubview:label];
label.backgroundColor = [UIColor redColor];
label.text = @"我显示出来了";
label.tag = 1000;
label.alpha = 0.0;
// 显示或隐藏标签
- (void)showLable:(UISwitch *)switchView
{
BOOL isShow = switchView.on;
UILabel *label = (UILabel *)[self.view viewWithTag:1000];
if (isShow)
{
label.text = @"我显示出来了";
label.alpha = 1.0;
}
else
{
label.text = @"我要隐身了";
[UIView animateWithDuration:1.2 animations:^{
label.alpha = 0.0;
}];
}
}