UISwitch

来源:互联网 发布:手机主板检测软件 编辑:程序博客网 时间:2024/04/30 12:19

UISwitch左右滑动关闭,或开启的开机按钮视图控件

基本属性

// 1 初始化UISwitch *switchView = [[UISwitch alloc] init];// 2 添加到父视图[self.view addSubview:switchView];// 3 设置原点坐标及大小(通常情况下只设置原点坐标,大小默认(frame = (0 0;51 31)))// 3-1 方法1//    switchView.frame = CGRectMake(50.0,50.0,51.0,31.0);    // 3-2 方法2CGRect rect = switchView.frame;rect.origin.x = 50.0;rect.origin.y = 50.0;switchView.frame = rect;NSLog(@"switchView %@",switchView);
初始化,开启后默认背景颜色为绿色


其他属性

// 1 设置开启后的背景颜色switchView.onTintColor = [UIColor redColor];// 2 设置关闭后的边框颜色switchView.tintColor = [UIColor orangeColor];// 3 设置拔弄开关的颜色switchView.thumbTintColor = [UIColor purpleColor];// 4 设置开关状态,默认是NO,即关闭switchView.on = NO;


添加响应方法

[switchView addTarget:self action:@selector(showLabel:) forControlEvents:UIControlEventValueChanged];    // 1 设置Lable// 1.1 初始化UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50.0, 100.0, (CGRectGetWidth(self.view.bounds) - 50.0 * 2), 40.0)];// 1.2 将添加到父视图[self.view addSubview:label];// 1.3 设置背景颜色label.backgroundColor = [UIColor redColor];// 1.4 添加显示内容label.text = @"我显示出来了";// 1.5 tag值label.tag = 1000;// 1.6 透明度label.alpha = 0.0;


// 显示或隐藏标签- (void)showLabel:(UISwitch *)switchView{    BOOL isShow = switchView.on;    UILabel *label = (UILabel *)[self.view viewWithTag:1000.0];    if (isShow) {        label.text = @"我显示出来了";        label.alpha = 1.0;    }    else    {        label.text = @"我要隐身了";        [UIView animateWithDuration:1.2 animations:^{label.alpha = 0.5 ; } ];    }}









1 0
原创粉丝点击