UISwitch的使用

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;
        }];
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

番薯大佬

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值