欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

UISwitch的使用

发布时间:2023/12/20 68 豆豆
生活随笔 收集整理的这篇文章主要介绍了 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;}];} }


总结

以上是生活随笔为你收集整理的UISwitch的使用的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。