論 Swift 開發入門 : 開關(UISwitch)

來源:互聯網
上載者:User

標籤:swift   ios   開關   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

論 Swift 開發入門 : 開關(UISwitch)

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.