工作之余,学习下swift大法.把自己的学习过程分享一下.当中的布局很乱,就表在意这些细节了.直接上代码:
UIProgressView+NSTimer+UIstepper
UIStepper
UIProgressView
1 // 2 // ViewController.swift 3 // UIProgressView 4 // 5 // Created by shaoting on 16/3/24. 6 // Copyright © 2016年 9elephas. All rights reserved. 7 // swift控件学习篇 8 // UIProgressView 9 // NSTimer 10 // UIstepper 11 // 12 // 13 import UIKit 14 15 class ViewController: UIViewController { 16 var button:UIButton! 17 var progressView:UIProgressView! 18 var timer:NSTimer! 19 20 var stepper:UIStepper! 21 var label:UILabel! 22 override func viewDidLoad() { 23 super.viewDidLoad() 24 makeProgress() //进度条 25 makeStepper() //步 26 27 button = UIButton(frame: CGRect(x: self.view.frame.width/2 - 50, y: 50, 100, height: 50)) 28 button.setTitle("开始", forState: UIControlState.Normal) 29 button.addTarget(self, action: Selector("btnOnclick"), forControlEvents: UIControlEvents.AllTouchEvents) 30 button.backgroundColor = UIColor.grayColor() 31 self.view.addSubview(button) 32 33 label = UILabel(frame: CGRect(x: 50, y: self.view.frame.height/2, 100, height: 50)) 34 label.backgroundColor = UIColor.brownColor() 35 label.textColor = UIColor.blackColor() 36 label.textAlignment = .Center 37 label.text = "5.0" 38 self.view.addSubview(label); 39 // Do any additional setup after loading the view, typically from a nib. 40 } 41 42 func makeStepper(){ 43 stepper = UIStepper(frame: CGRect(x: 100, y: self.view.frame.height/2+60, 200, height: 100)) 44 stepper.tintColor = UIColor.blueColor() 45 stepper.value = 5 //默认值 46 stepper.minimumValue = 0 //最小值 47 stepper.maximumValue = 10 //最大值 48 stepper.stepValue = 1.0 //增量 49 stepper.autorepeat = true //设置是否允许按住不放增量 50 stepper.continuous = true 51 stepper.wraps = true //是否循环 52 stepper.addTarget(self, action: Selector("stepperValueChange"), forControlEvents: UIControlEvents.TouchUpInside) 53 self.view.addSubview(stepper) 54 } 55 func stepperValueChange(){ 56 label.text = "(stepper.value)" 57 } 58 59 60 61 func makeProgress(){ 62 progressView = UIProgressView(frame: CGRect(x: 20, y: 200, 120, height: 100)) 63 progressView.progressViewStyle = .Bar //类型 64 progressView.progress = 0.0 //初始值 65 progressView.progressTintColor = UIColor.blueColor() //走过进度条颜色 66 progressView.trackTintColor = UIColor.greenColor() //未走进度条颜色 67 self.view.addSubview(progressView) 68 } 69 func btnOnclick(){ 70 button.enabled = false 71 timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector:"timerAction", userInfo: nil, repeats: true) 72 timer.fire() 73 } 74 func timerAction(){ 75 progressView.progress += 0.02 76 if progressView.progress == 1{ 77 progressView.setProgress(0, animated: true) 78 } 79 } 80 81 82 override func didReceiveMemoryWarning() { 83 super.didReceiveMemoryWarning() 84 // Dispose of any resources that can be recreated. 85 } 86 87 88 }
UIAlertController
1 // 2 // ViewController.swift 3 // UIAlertController 4 // 5 // Created by shaoting on 16/3/25. 6 // Copyright © 2016年 9elephas. All rights reserved. 7 // swift控件学习篇 8 // alert 9 10 // 11 // 12 13 14 import UIKit 15 16 class ViewController: UIViewController { 17 18 var alert1:UIAlertController! 19 var alert2:UIAlertController! 20 var actionSheet:UIAlertController! 21 22 var cancelAction = UIAlertAction!() 23 var okAction = UIAlertAction!() 24 var deleteAction = UIAlertAction!() 25 26 override func viewDidLoad() { 27 super.viewDidLoad() 28 // 定义一个按钮,用于点击显示最简单的Alert 29 let button1 = UIButton(type: UIButtonType.System) 30 button1.frame = CGRect(x: 20, y: 20, 400, height: 50) 31 button1.setTitle("最简单的Alert", forState: UIControlState.Normal) 32 button1.addTarget(self, action: Selector("action1"), forControlEvents: UIControlEvents.TouchUpInside) 33 self.view.addSubview(button1) 34 // 定义一个按钮,用于点击显示带文本输入框的Alert 35 let button2:UIButton = UIButton(type: UIButtonType.System) 36 button2.frame = CGRect(x: 20, y: 100, 400, height: 50) 37 button2.setTitle("带文本输入框的Alert", forState: UIControlState.Normal) 38 button2.addTarget(self, action: Selector("action2"), forControlEvents: UIControlEvents.TouchUpInside) 39 self.view.addSubview(button2) 40 // 定义一个按钮,用于点击显示上拉菜单 41 let button3 = UIButton(type: UIButtonType.System) 42 button3.frame = CGRect(x: 20, y: 160, 400, height: 50) 43 button3.setTitle("上拉菜单", forState: UIControlState.Normal) 44 button3.addTarget(self, action: Selector("action3"), forControlEvents: UIControlEvents.TouchUpInside) 45 self.view.addSubview(button3) 46 47 48 49 50 51 // Do any additional setup after loading the view, typically from a nib. 52 } 53 54 func action1(){ 55 //定义菜单按钮 56 cancelAction = UIAlertAction(title: "取消", style: UIAlertActionStyle.Cancel, handler: nil) 57 okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: { (UIAlertAction) -> Void in 58 print("you choose OK") 59 }) 60 deleteAction = UIAlertAction(title: "删除", style: UIAlertActionStyle.Destructive, handler: { (UIAlertAction) -> Void in 61 print("you choose delete") 62 }) 63 //定义一个按钮,显示带文本框的Alert 64 alert1 = UIAlertController(title: "最简单的Alert", message: "this is a simple", preferredStyle: UIAlertControllerStyle.Alert) 65 alert1.addAction(cancelAction) 66 alert1.addAction(okAction) 67 alert1.addAction(deleteAction) 68 self.presentViewController(alert1, animated: true, completion: nil) 69 } 70 func action2(){ 71 alert2 = UIAlertController(title: "带输入框的alert", message: "this is a test alert", preferredStyle: UIAlertControllerStyle.Alert) 72 alert2.addTextFieldWithConfigurationHandler {(textFiled:UITextField!) -> Void in 73 textFiled.placeholder = "username" 74 } 75 alert2.addTextFieldWithConfigurationHandler { (textFiled:UITextField) -> Void in 76 textFiled.placeholder = "password" 77 textFiled.secureTextEntry = true 78 } 79 let loginAction = UIAlertAction(title: "login", style: UIAlertActionStyle.Default) { (action:UIAlertAction!) -> Void in 80 let name = (self.alert2.textFields?.first!)! as UITextField 81 let password = (self.alert2.textFields?.last)! as UITextField 82 print("name:(name.text);password:(password.text)") 83 } 84 alert2.addAction(loginAction) 85 self.presentViewController(alert2, animated: true, completion: nil) 86 } 87 func action3(){ 88 actionSheet = UIAlertController(title: "上拉菜单", message: "this is a action", preferredStyle: UIAlertControllerStyle.ActionSheet) 89 cancelAction = UIAlertAction(title: "取消", style: UIAlertActionStyle.Cancel, handler: nil) 90 okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: { (UIAlertAction) -> Void in 91 print("you choose OK") 92 }) 93 deleteAction = UIAlertAction(title: "删除", style: UIAlertActionStyle.Destructive, handler: { (UIAlertAction) -> Void in 94 print("you choose delete") 95 }) 96 actionSheet.addAction(okAction) 97 actionSheet.addAction(cancelAction) 98 actionSheet.addAction(deleteAction) 99 self.presentViewController(actionSheet, animated: true, completion: nil) 100 101 102 } 103 104 105 106 override func didReceiveMemoryWarning() { 107 super.didReceiveMemoryWarning() 108 // Dispose of any resources that can be recreated. 109 } 110 111 112 }
UIProgressView+NSTimer+UIstepper源码下载:
http://download.csdn.net/detail/shaoting19910730/9471731
https://github.com/pheromone/swift-UIAlertController-
UIAlertController源码下载;
http://download.csdn.net/detail/shaoting19910730/9472635
https://github.com/pheromone/UIProgressView-NSTimer-UIstepper
学习网站: