微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

[Swift]UIKit学习之滑块控件UISwitch的用法

UIKit学习之开关控件UISwitch的用法


UISwitch是一个开关控件。

UISwitch控件的创建:

(1) 在Stroyboard中使用Ctrl+Drag拖拽法创建

(2) 代码创建UISwitch:

letmySwitch=UISwitch()
//设置位置(开关大小无法设置)
mySwitch.center=CGPointMake(100,50);
//设置认值(开true/关false)
mySwitch.on=true;

self.view.addSubview(mySwitch);


代码实例:

//ViewController.swift
importUIKit
classViewController:UIViewController{
overridefuncviewDidLoad(){
super.viewDidLoad()
//Doanyadditionalsetupafterloadingtheview,typicallyfromanib.

letmySwitch=UISwitch()
//设置位置(开关大小无法设置)
mySwitch.center=CGPointMake(100,50);
//设置认值(开true/关false)
mySwitch.on=true;
mySwitch.addTarget(self,action:"switchDidChanged:",forControlEvents:UIControlEvents.ValueChanged)

self.view.addSubview(mySwitch);
}

//
funcswitchDidChanged(sender:UISwitch){
if(sender.on){
self.view.backgroundColor=UIColor.brownColor()
myLabel.text="开关已经打开";
myLabel.backgroundColor=UIColor.blueColor()

}else{
self.view.backgroundColor=UIColor.blackColor()
myLabel.text="开关已经关闭"
myLabel.backgroundColor=UIColor.greenColor()
}
}

overridefuncdidReceiveMemoryWarning(){
super.didReceiveMemoryWarning()
//dispoSEOfanyresourcesthatcanberecreated.
}
}


开发环境:

XcodeVersion 7.0 (7A220)


参考资料:

http://www.hangge.com/blog/cache/detail_532.html

http://www.chuanke.com/1266915-124765.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐