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

前端之家收集整理的这篇文章主要介绍了[Swift]UIKit学习之滑块控件UISwitch的用法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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


UISwitch是一个开关控件。

UISwitch控件的创建:

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

(2) 代码创建UISwitch:

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


代码实例:

@H_403_28@//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

原文链接:https://www.f2er.com/swift/325729.html

猜你在找的Swift相关文章