UI04_UISwitch
2015-08-05 08:29
316 查看
它是一个开关按钮控件,可以通过它来设置我们的视图或者音频的开闭状态
1.创建UISwitch对象
UISwitch *mySwitch=[[UISwitch alloc]initWithFrame:CGRectMake(200, 100, 100, 30)]; [self.view addSubview:mySwitch];
2.设置开启颜色
mySwitch.onTintColor=[UIColor blueColor];
3.设置开启图片;
mySwitch.onImage=[UIImage imageNamed:@"BtnOn.png"];
4.设置关闭图片
mySwitch.offImage=[UIImage imageNamed:@"BtnOff.png"];
5.设置正常关闭颜色
mySwitch.tintColor=[UIColor redColor];
6.设置animated动画效果
通过设置YES或NO,是否使用animated动画效果: [mySwitch setOn:YES animated:YES];
7.查看开闭状态
(1)获取对象的isOn属性,默认是关闭状态; (2)如果isOn==YES则是开启状态,如果isOn==NO则是关闭状态。 (3)获取UISwitch的关闭状态,注默认关闭 if(mySwitch.isOn){ NSLog(@"开启状态"); }else{ NSLog(@"关闭状态"); }
8.添加动作事件
[mySwitch addTarget:self action:@selector(switchChange:) forControlEvents:UIControlEventValueChanged]; //可以选择id类也乐意选择UISwitch,如果是id类型要转换成UISwitch //*mySwitch=(UISwitch *)sender -(void)switchChange:(id)sender{ UISwitch *mySwitch=(UISwitch *)sender; if (mySwitch.isOn) { NSLog(@"dsfsgsg"); }else{ NSLog(@"guabci"); } }
相关文章推荐
- UI之窗口与视图
- 我的第一个UI程序
- UIKit 手势识别 UIGestureRecognizer
- UIKit 视图 UIView
- HDU 4343 Interval query(倍增思想+贪心)
- Installing Ubuntu on a Pre-Installed Windows 8 (64-bit) System (UEFI Supported)
- Windows和Ubuntu双系统,修复UEFI引导的两种办法
- Leetcode #95 Unique Binary Search Trees II
- Leetcode # 96 Unique Binary Search Trees
- 无法启动soapUI 提示:the jvm could not be started. the main method may have thrown an exception
- CodeForces 91B-K - Queue 二分
- [IOS]今天开始学UI---UIAlertController
- hdu4918 Query on the subtree
- UE4信息交互-蓝图类之间的信息交互
- 循环结构中break、continue、return和exit的区别
- [IOS]今天开始学UI---UIActivityIndicatorView
- PostgreSQL: Query for location of global tablespace?
- 【HDU4632】【Palindrome subsequence】
- iOS UIImageView 添加 UITapGestureRecognizer 事件
- install build essential