在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
工作之余,学习下swift大法.把自己的学习过程分享一下.当中的布局很乱,就表在意这些细节了.直接上代码: UIButton+UILabel 1 // 2 // ViewController.swift 3 // manyControl 4 // 5 // Created by shaoting on 16/3/23. 6 // Copyright © 2016年 9elephas. All rights reserved. 7 // 8 /// swift控件篇 9 // UIButton 10 // UILabel 11 import UIKit 12 13 class ViewController: UIViewController { 14 15 override func viewDidLoad() { 16 super.viewDidLoad() 17 18 makebtn(); //按钮 19 makelabel(); //UILabel 20 // Do any additional setup after loading the view, typically from a nib. 21 } 22 func makebtn(){ 23 //按钮button 24 let btn:UIButton = UIButton(type:.Custom) UITextField+UISwitch+UISlider+UIimageView 1 // 2 // ViewController.swift 3 // UITextField+UIImage 4 // 5 // Created by shaoting on 16/3/24. 6 // Copyright © 2016年 9elephas. All rights reserved. 7 // 8 // swift 控件学习篇 9 // UITextFiled 10 // UISwitch 11 // UISlider 12 // UIimageView 13 import UIKit 14 15 class ViewController: UIViewController,UITextFieldDelegate { 16 var textField:UITextField! 17 var switch1:UISwitch! 18 var slider:UISlider! 19 var imageView:UIImageView! 20 override func viewDidLoad() { 21 super.viewDidLoad() 22 makeTextField(); 23 makeUISwitch(); 24 makeSlide(); 25 26 let btn = UIButton(frame: CGRect(x: 200, y: 150, width: 100, height: 50)) 27 btn.setTitle("复位", forState: .Normal); 28 btn.addTarget(self, action: Selector("recover:"), forControlEvents: UIControlEvents.AllEvents) 29 btn.backgroundColor = UIColor.redColor() 30 btn.tag = 20 31 self.view.addSubview(btn) 32 33 imageView = UIImageView(frame: CGRect(x: 200, y: 400, width: 50, height: 50)) 34 imageView.image = UIImage(named: "stop") 35 self.view.addSubview(imageView); 36 // Do any additional setup after loading the view, typically from a nib. 37 } 38 39 func makeSlide(){ 40 slider = UISlider(frame: CGRect(x: 20, y: 200, width: 400, height: 200)) 41 slider.minimumValue = 0 //最小值 42 slider.maximumValue = 100 //最大值 43 slider.value = 20 //默认值 44 slider.minimumValueImage = UIImage(named: "start") //最小值图片 45 slider.maximumValueImage = UIImage(named: "stop") //最大值图片 46 // slider.setMinimumTrackImage(UIImage(named: "left"), forState: UIControlState.Normal)//设置滑块左边的轨道图片 47 // slider.setMaximumTrackImage(UIImage(named: "right"), forState: UIControlState.Normal)//设置滑块右边的轨道图片 48 slider.setThumbImage(UIImage(named: "center"), forState: UIControlState.Normal)//设置滑块图片 49 slider.minimumTrackTintColor = UIColor.brownColor() //划过的轨道颜色 50 slider.maximumTrackTintColor = UIColor.yellowColor() //未划过的轨道颜色 51 52 self.view.addSubview(slider); 53 } 54 func recover(sender:UIButton){ 55 slider.setValue(20, animated: true) 56 } 57 58 func makeUISwitch(){ 59 switch1 = UISwitch(frame: CGRect(x: 30, y: 150, width: 400, height: 10)) 60 switch1.on = true //默认ON是否打开 61 switch1.tintColor = UIColor.redColor() 62 switch1.onTintColor = UIColor.yellowColor() 63 switch1.thumbTintColor = UIColor.blackColor() 64 switch1.addTarget(self, action: Selector("switchChane:"), forControlEvents: UIControlEvents.AllEvents) 65 self.view.addSubview(switch1) 66 } 67 func switchChane(senger:UISwitch){ 68 if senger.on{ 69 print("is ON") 70 }else{ 71 print("is OFF") 72 } 73 } 74 75 76 77 78 79 func makeTextField(){ 80 textField = UITextField(frame: CGRect(x: 30, y: 50, width: self.view.frame.size.width-60, height: 70)) 81 // textField.borderStyle = UITextBorderStyle.RoundedRect 82 textField.borderStyle = .Line //边框样式 83 textField.placeholder = "请输入内容" //占位符 84 textField.text = "username" //为textFiled设置默认文字 85 textField.delegate = self 86 textField.keyboardType = .EmailAddress //键盘类型 87 self.view.addSubview(textField) 88 } 89 //UITextFieldDelegate中的方法 90 func textFieldDidBeginEditing(textField: UITextField) { 91 print("开始编辑") 92 } 93 func textFieldDidEndEditing(textField: UITextField) { 94 print("结束编辑") 95 } 96 //点击return按钮隐藏键盘 97 func textFieldShouldReturn(textField: UITextField) -> Bool { 98 textField.resignFirstResponder() 99 return true 100 } 101 //点击编辑框外部,隐藏键盘 102 override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) { 103 textField.resignFirstResponder() 104 } 105 106 override func didReceiveMemoryWarning() { 107 super.didReceiveMemoryWarning() 108 // Dispose of any resources that can be recreated. 109 } 110 111 112 113 114 } 源码下载地址: UIButton+UILabel http://download.csdn.net/detail/shaoting19910730/9471728 https://github.com/pheromone/UIButton-UILabel UITextField+UISwitch+UISlider+UIimageView: http://download.csdn.net/detail/shaoting19910730/9471731 https://github.com/pheromone/UITextField-UISwitch-UISlider-UIimageView swift 学习网站 |
请发表评论