IOS 7 开发范例 - UISwitch的使用

简介: Creating and Using Switches with UISwitch You would like to give your users the ability to turn an option on or off.

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:

 

 

 

 

 

目录
相关文章
|
2月前
|
开发框架 前端开发 Android开发
安卓与iOS开发中的跨平台策略
在移动应用开发的战场上,安卓和iOS两大阵营各据一方。随着技术的演进,跨平台开发框架成为开发者的新宠,旨在实现一次编码、多平台部署的梦想。本文将探讨跨平台开发的优势与挑战,并分享实用的开发技巧,帮助开发者在安卓和iOS的世界中游刃有余。
|
24天前
|
iOS开发 开发者 MacOS
深入探索iOS开发中的SwiftUI框架
【10月更文挑战第21天】 本文将带领读者深入了解Apple最新推出的SwiftUI框架,这一革命性的用户界面构建工具为iOS开发者提供了一种声明式、高效且直观的方式来创建复杂的用户界面。通过分析SwiftUI的核心概念、主要特性以及在实际项目中的应用示例,我们将展示如何利用SwiftUI简化UI代码,提高开发效率,并保持应用程序的高性能和响应性。无论你是iOS开发的新手还是有经验的开发者,本文都将为你提供宝贵的见解和实用的指导。
118 66
|
10天前
|
存储 监控 API
app开发之安卓Android+苹果ios打包所有权限对应解释列表【长期更新】-以及默认打包自动添加权限列表和简化后的基本打包权限列表以uniapp为例-优雅草央千澈
app开发之安卓Android+苹果ios打包所有权限对应解释列表【长期更新】-以及默认打包自动添加权限列表和简化后的基本打包权限列表以uniapp为例-优雅草央千澈
|
1月前
|
开发框架 Android开发 iOS开发
安卓与iOS开发中的跨平台策略:一次编码,多平台部署
在移动应用开发的广阔天地中,安卓和iOS两大阵营各占一方。随着技术的发展,跨平台开发框架应运而生,它们承诺着“一次编码,到处运行”的便捷。本文将深入探讨跨平台开发的现状、挑战以及未来趋势,同时通过代码示例揭示跨平台工具的实际运用。
105 3
|
1月前
|
Java 调度 Android开发
安卓与iOS开发中的线程管理差异解析
在移动应用开发的广阔天地中,安卓和iOS两大平台各自拥有独特的魅力。如同东西方文化的差异,它们在处理多线程任务时也展现出不同的哲学。本文将带你穿梭于这两个平台之间,比较它们在线程管理上的核心理念、实现方式及性能考量,助你成为跨平台的编程高手。
|
2月前
|
存储 前端开发 Swift
探索iOS开发:从新手到专家的旅程
本文将带您领略iOS开发的奇妙之旅,从基础概念的理解到高级技巧的掌握,逐步深入iOS的世界。文章不仅分享技术知识,还鼓励读者在编程之路上保持好奇心和创新精神,实现个人成长与技术突破。
|
2月前
|
安全 IDE Swift
探索iOS开发之旅:从初学者到专家
在这篇文章中,我们将一起踏上iOS开发的旅程,从基础概念的理解到深入掌握核心技术。无论你是编程新手还是希望提升技能的开发者,这里都有你需要的指南和启示。我们将通过实际案例和代码示例,展示如何构建一个功能齐全的iOS应用。准备好了吗?让我们一起开始吧!
|
2月前
|
安全 Swift iOS开发
Swift 与 UIKit 在 iOS 应用界面开发中的关键技术和实践方法
本文深入探讨了 Swift 与 UIKit 在 iOS 应用界面开发中的关键技术和实践方法。Swift 以其简洁、高效和类型安全的特点,结合 UIKit 丰富的组件和功能,为开发者提供了强大的工具。文章从 Swift 的语法优势、类型安全、编程模型以及与 UIKit 的集成,到 UIKit 的主要组件和功能,再到构建界面的实践技巧和实际案例分析,全面介绍了如何利用这些技术创建高质量的用户界面。
38 2
|
2月前
|
安全 数据处理 Swift
深入探索iOS开发中的Swift语言特性
本文旨在为开发者提供对Swift语言在iOS平台开发的深度理解,涵盖从基础语法到高级特性的全面分析。通过具体案例和代码示例,揭示Swift如何简化编程过程、提高代码效率,并促进iOS应用的创新。文章不仅适合初学者作为入门指南,也适合有经验的开发者深化对Swift语言的认识。
60 9
|
2月前
|
vr&ar Android开发 iOS开发
安卓与iOS开发中的用户界面设计原则
【10月更文挑战第41天】探索移动应用开发的精髓,本文将深入分析安卓和iOS平台上用户界面设计的核心原则。通过比较两大操作系统的设计哲学,我们将揭示如何打造直观、易用且美观的应用程序界面。无论你是初学者还是资深开发者,这篇文章都将为你提供宝贵的见解和实用的技巧,帮助你在竞争激烈的应用市场中脱颖而出。