论 Swift 开发入门 : 开关(UISwitch)

转载请声明出处:http://blog.csdn.net/jinnchang/article/details/44407193

1、UIButton 概述

继承关系:UISwitch -> UIControl -> UIView

2、控件样式

3、使用示例

var myButton: UIButton?
var mySwitch: UISwitch?

override func viewDidLoad() {

    self.myButton = UIButton.buttonWithType(.System) as? UIButton
    self.myButton!.frame = CGRectMake(self.view.frame.width/2 - 100, 200, 200, 100)
    self.myButton!.setTitle("change state", forState: UIControlState.Normal)
    self.myButton!.addTarget(self, action: "buttonAction", forControlEvents: UIControlEvents.TouchUpInside)

    self.mySwitch = UISwitch(frame:CGRectMake(self.view.frame.width/2 - 20, 400, 10, 100))
    self.mySwitch!.on = true
    self.mySwitch!.onTintColor = UIColor.lightGrayColor()
    self.mySwitch!.tintColor = UIColor.greenColor()
    self.mySwitch!.thumbTintColor = UIColor.blackColor()
    self.mySwitch!.addTarget(self,action:Selector("switchChange:"), forControlEvents: UIControlEvents.ValueChanged)
    // 扩展:还可以通过设置 onImage、offImage 来来添加图片

    self.view.addSubview(self.myButton!)
    self.view.addSubview(self.mySwitch!)
}

/// 按钮相应事件
func buttonAction() {
    if self.mySwitch!.on{
        println("Switch is on")
        self.mySwitch!.setOn(false, animated:true)
    }else{
        println("Switch is off")
        self.mySwitch!.setOn(true, animated:true)
    }
}

/// 开关控制事件
func switchChange(switchState: UISwitch) {
    if switchState.on {
        println("Switch is on")
    } else {
        println("Switch is off")
    }
}

4、结语

Github 上项目地址:UISwitchSample

文章最后更新时间:2015年3月18日10:58:09。参考资料如下:

UIKit User
Interface Catalog: Switches

UISwitch Class Reference

时间: 2024-07-29 22:06:26

论 Swift 开发入门 : 开关(UISwitch)的相关文章

[连载]Swift开发入门(05)--- 枚举

??枚举是定义符号常量的手段,它把一堆相似的值组织在一起.例如你在指定文字对齐方式的时候通常有三种可选的值:左对齐.右对齐和居中对齐:你在处理游戏中的方法时可能的取值有东.西.南.北.Swift中的枚举比你了解的其他语言的枚举更加强大,它的行为类似于类和结构体,它甚至可以有自己的方法,包括构造器. 创建枚举 ??我们先通过一个简单的例子来认识一下枚举.玩过扑克牌的都知道,扑克有四种花色,分别是黑桃(spade).红心(heart).草花(club)和方块(diamond),如果我们要做一个扑克游

论 Swift 开发入门:搜索框(UISearchBar)

转载请声明出处:http://blog.csdn.net/jinnchang/article/details/44827923 ------------------------------------------------------------------------------------------ 代码示例 // // ViewController.swift // UISearchBarSample // // Created by jinnchang on 15/4/1. // C

论 Swift 开发入门:活动指示器(UIActivityIndicatorView)

转载请声明出处:http://blog.csdn.net/jinnchang/article/details/44828021 ------------------------------------------------------------------------------------------ 代码示例 // // ViewController.swift // UIActivityIndicatorViewSample // // Created by jinnchang on

论 Swift 开发入门 : 选择器(UIPickerView)

转载请声明出处:http://blog.csdn.net/jinnchang/article/details/44487269 ------------------------------------------------------------------------------------------ 1.Summary -------------------------------------------------------------------------------------

论 Swift 开发入门 : 滑块(UISlider)

转载请声明出处:http://blog.csdn.net/jinnchang/article/details/44487407 ------------------------------------------------------------------------------------------ 1.Summary -------------------------------------------------------------------------------------

[连载]Swift开发入门(06)--- 函数式编程

??面向对象编程和函数式编程是目前最主流的两种编程范式,而关于这两种范式孰优孰劣的讨论一直都没有停止过.事实上,真正理解两种编程范式的程序员不会武断的说这二者孰优孰劣,因为任何编程语言都没有什么灵丹妙药让其使用者成为优秀的程序员.其实,像Java这样很经典的面向对象的编程语言,也能够看到函数式编程的影子,如果你使用过访问者模式.命令模式,如果你使用过接口回调,你实际上已经使用了函数式编程的理念,而且在新版本的Java中,已经开始支持Lambda表达式和函数式接口,这些都是Java为了支持函数式编

论 Swift 开发入门 : 进度条(UIProgressView)

转载请声明出处:http://blog.csdn.net/jinnchang/article/details/44802019 ------------------------------------------------------------------------------------------ 概述 ------------------------------------------------------------------------------------------ 代

论 Swift 开发入门 : 按钮(UIButton)

转载请声明出处:http://blog.csdn.net/jinnchang/article/details/44403537 1.UIButton 概述 继承关系:UIButton -> UIControl -> UIView 控件样式: 2.UIButton 初始化 (1)使用 buttonWithType 构建按钮,已有的六种类型如下: enum UIButtonType : Int { case Custom // 自定义风格 case System // 圆角矩形 case Deta

Swift开发语法

Swift开发入门 简介 Swift 语言由苹果公司在 2014 年推出,用来撰写 OS X 和 iOS 应用程序 2014 年,在 Apple WWDC 发布 历史 2010 年 7 月,苹果开发者工具部门总监 Chris Lattner(克里斯·拉特纳) 开始着手 Swift 编程语言的设计工作 用一年时间,完成基本架构 Swift 大约历经 4 年的开发期,2014 年 6 月发表 克里斯·拉特纳 ) 特色 苹果宣称 Swift 的特点是:快速.现代.安全.互动,而且明显优于 Object