IOS 7 开发范例 - UISwitch的使用
2014-02-09 15:51
232 查看
Creating and Using Switches with UISwitch
You would like to give your users the ability to turn an option on or off.
effect as follow:
Solution
Use the UISwitch class.
Some steps to implment:
1. Let’s create a property of type UISwitch and call it mainSwitch
ViewController.m:
2. the viewDidLoad method in your view controller’s implementation file,
Let’s create our switch and place it on our view controller’s view
Note that the parameter that we have to pass to this method is
of type CGRect. A CGRect denotes the boundaries of a rectangle using the (x,y) position
of the top-left corner of the rectangle and its width and height.
After we’ve created the switch, we simply add it to our view controller’s view.
// set the switch on
[self.mainSwitch setOn:YES];
you can read from the on property of the switch to find out whether the
switch is on or off at the moment.
Alternatively, you can use the isOn method of the switch.
eg:
If you want to get notified when the switch gets turned on or off, you will need to add
your class as the target for the switch, using the addTarget:action:forControlEvents:
method of UISwitch
eg:
Then implement the switchIsChanged: method.
Run Result:
Sender is = <UISwitch: 0x6e13500;
frame = (100 100; 79 27);
layer = <CALayer: 0x6e13700>>
The switch is turned off.
Customizing the UISwitch
There are two main ways of customizing a switch:
Tint Colors
Tint colors are colors that you can apply to a UI component such as a UISwitch.
The tint color will be applied on top of the current color of the component. For
instance, in a normal UISwitch, you will be able to see different colors. When you
apply the tint color on top, the normal color of the control will be mixed with the
tint color, giving a flavor of the tint color on the UI control.
Images
A switch has two images:
On Image
The image that represents the on state of the switch. The width of this image is
77 points, and its height is 22.
Off Image
The image that represents the switch in its off state. This image, like the on state
of the switch, is 77 points in width and 22 points in height.
tintColor
This is the tint color that will be applied to the off state of the switch.
Unfortunately, Apple has not taken the time to name this property offTintColor instead of tint
Color to make it more explicit.
thumbTintColor
This is the tint color that will be applied to the little knob on the switch.
onTintColor
This tint color will be applied to the switch in its on state.
// simple code snippet that will change the on-mode tint color of the switch to
// red, the off-mode tint color to brown, and the knob’s tint color to green.
Let’s move on to customizing the appearance of the switch using its on and off images
prepare a new set of on and off images.
As mentioned before, both the on and the off images in a switch should be 77 points wide and 22 points tall.
On@2x.png
Off@2x.png
eg:
You would like to give your users the ability to turn an option on or off.
effect as follow:
Solution
Use the UISwitch class.
Some steps to implment:
1. Let’s create a property of type UISwitch and call it mainSwitch
ViewController.m:
#import "ViewController.h" @interface ViewController () @property (nonatomic, strong) UISwitch *mainSwitch; @end @implementation ViewController ...
2. the viewDidLoad method in your view controller’s implementation file,
Let’s create our switch and place it on our view controller’s view
- (void)viewDidLoad{ [super viewDidLoad]; /* Create the switch */ self.mySwitch = [[UISwitch alloc] initWithFrame: CGRectMake(100, 100, 0, 0)]; [self.mySwitch setOn:YES]; [self.view addSubview:self.mySwitch]; [self.mySwitch addTarget:self action:@selector(switchIsChanged:) forControlEvents:UIControlEventValueChanged];}
Note that the parameter that we have to pass to this method is
of type CGRect. A CGRect denotes the boundaries of a rectangle using the (x,y) position
of the top-left corner of the rectangle and its width and height.
After we’ve created the switch, we simply add it to our view controller’s view.
// set the switch on
[self.mainSwitch setOn:YES];
you can read from the on property of the switch to find out whether the
switch is on or off at the moment.
Alternatively, you can use the isOn method of the switch.
eg:
if ([self.mainSwitch isOn]){ NSLog(@"The switch is on."); } else { NSLog(@"The switch is off."); }
If you want to get notified when the switch gets turned on or off, you will need to add
your class as the target for the switch, using the addTarget:action:forControlEvents:
method of UISwitch
eg:
[self.mainSwitch addTarget:self action:@selector(switchIsChanged:) forControlEvents:UIControlEventValueChanged];
Then implement the switchIsChanged: method.
- (void) switchIsChanged:(UISwitch *)paramSender{ NSLog(@"Sender is = %@", paramSender); if ([paramSender isOn]){ NSLog(@"The switch is turned on."); } else { NSLog(@"The switch is turned off."); } }
Run Result:
Sender is = <UISwitch: 0x6e13500;
frame = (100 100; 79 27);
layer = <CALayer: 0x6e13700>>
The switch is turned off.
Customizing the UISwitch
There are two main ways of customizing a switch:
Tint Colors
Tint colors are colors that you can apply to a UI component such as a UISwitch.
The tint color will be applied on top of the current color of the component. For
instance, in a normal UISwitch, you will be able to see different colors. When you
apply the tint color on top, the normal color of the control will be mixed with the
tint color, giving a flavor of the tint color on the UI control.
Images
A switch has two images:
On Image
The image that represents the on state of the switch. The width of this image is
77 points, and its height is 22.
Off Image
The image that represents the switch in its off state. This image, like the on state
of the switch, is 77 points in width and 22 points in height.
tintColor
This is the tint color that will be applied to the off state of the switch.
Unfortunately, Apple has not taken the time to name this property offTintColor instead of tint
Color to make it more explicit.
thumbTintColor
This is the tint color that will be applied to the little knob on the switch.
onTintColor
This tint color will be applied to the switch in its on state.
// simple code snippet that will change the on-mode tint color of the switch to
// red, the off-mode tint color to brown, and the knob’s tint color to green.
- (void)viewDidLoad { [super viewDidLoad]; /* Create the switch */ self.mainSwitch = [[UISwitch alloc] initWithFrame:CGRectZero]; self.mainSwitch.center = self.view.center; [self.view addSubview:self.mainSwitch]; /* Customize the switch */ /* Adjust the off-mode tint color */ self.mainSwitch.tintColor = [UIColor redColor]; /* Adjust the on-mode tint color */ self.mainSwitch.onTintColor = [UIColor brownColor]; /* Also change the knob's tint color */ self.mainSwitch.thumbTintColor = [UIColor greenColor]; }
Let’s move on to customizing the appearance of the switch using its on and off images
prepare a new set of on and off images.
As mentioned before, both the on and the off images in a switch should be 77 points wide and 22 points tall.
On@2x.png
Off@2x.png
- (void)viewDidLoad { [super viewDidLoad]; /* Create the switch */ self.mainSwitch = [[UISwitch alloc] initWithFrame:CGRectZero]; self.mainSwitch.center = self.view.center; /* Make sure the switch won't appear blurry(模糊) on iOS Simulator */ self.mainSwitch.frame = [self roundedValuesInRect:self.mainSwitch.frame]; [self.view addSubview:self.mainSwitch]; /* Customize the switch */ self.mainSwitch.onImage = [UIImage imageNamed:@"On"]; self.mainSwitch.offImage = [UIImage imageNamed:@"Off"]; }
eg:
相关文章推荐
- iOS开发之UitableViewCell中UISwitch的使用,代理实现
- iOS开发技巧 - 使用和定制开关控件(UISwitch)
- 【iOS开发-13】大神器:如何利用cocoapods使用第三方类库,以针对UISwitch的第三方类库为例
- iOS开发之UISwitch控件的使用
- iOS开发-UI控件:UISwitch控件两种使用方法和监听
- iOS开发技巧 - 使用和定制开关控件(UISwitch)
- iOS开发中UISwitch按钮的使用方法简介
- 【IOS开发】SimPholders的使用
- iOS开发:如何使用iOS手势UIGestureRecognizer
- 【iOS开发-存储】使用偏好设置preference存储
- iOS开发:使用Block在两个界面之间…
- iOS开发2-使用SAX解析XML数据
- 设置视图IOS开发(97)之自定义视图之使用XIB自定义一个UIView
- iOS开发:XIB使用技巧总结
- 【iOS开发必收藏】详解iOS应用程序内使用IAP/StoreKit付费、沙盒(SandBox)测试、创建测试账号流程!
- IOS--UISwitch的使用方法
- iOS开发之使用pjsip开发VoIP应用程序
- iOS开发之三:常用控件--UILabel的使用
- iOS开发之四:常用控件--UIButton的使用
- iOS开发-正则表达式的使用方法