IOS 7 开发范例 - UISwitch的使用

本文详细介绍如何在iOS应用中使用UISwitch类创建开关效果,包括设置开关状态、监听开关变化及自定义开关外观,如调整颜色和设置开关图片。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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:

#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:

 

 

 

 

 

转载于:https://www.cnblogs.com/davidgu/p/3541501.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值