工作之余,学习下swift大法.把自己的学习过程分享一下.当中的布局很乱,就表在意这些细节了.直接上代码:
UIProgressView+NSTimer+UIstepper
UIStepper
UIProgressView
//
// ViewController.swift
// UIProgressView
//
// Created by shaoting on 16/3/24.
// Copyright © 2016年 9elephas. All rights reserved.
// swift控件学习篇
// UIProgressView
// NSTimer
// UIstepper
//
//
import UIKit class ViewController: UIViewController {
var button:UIButton!
var progressView:UIProgressView!
var timer:NSTimer! var stepper:UIStepper!
var label:UILabel!
override func viewDidLoad() {
super.viewDidLoad()
makeProgress() //进度条
makeStepper() //步 button = UIButton(frame: CGRect(x: self.view.frame.width/ - , y: , width: , height: ))
button.setTitle("开始", forState: UIControlState.Normal)
button.addTarget(self, action: Selector("btnOnclick"), forControlEvents: UIControlEvents.AllTouchEvents)
button.backgroundColor = UIColor.grayColor()
self.view.addSubview(button) label = UILabel(frame: CGRect(x: , y: self.view.frame.height/, width: , height: ))
label.backgroundColor = UIColor.brownColor()
label.textColor = UIColor.blackColor()
label.textAlignment = .Center
label.text = "5.0"
self.view.addSubview(label);
// Do any additional setup after loading the view, typically from a nib.
} func makeStepper(){
stepper = UIStepper(frame: CGRect(x: , y: self.view.frame.height/+, width: , height: ))
stepper.tintColor = UIColor.blueColor()
stepper.value = //默认值
stepper.minimumValue = //最小值
stepper.maximumValue = //最大值
stepper.stepValue = 1.0 //增量
stepper.autorepeat = true //设置是否允许按住不放增量
stepper.continuous = true
stepper.wraps = true //是否循环
stepper.addTarget(self, action: Selector("stepperValueChange"), forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(stepper)
}
func stepperValueChange(){
label.text = "\(stepper.value)"
} func makeProgress(){
progressView = UIProgressView(frame: CGRect(x: , y: , width: , height: ))
progressView.progressViewStyle = .Bar //类型
progressView.progress = 0.0 //初始值
progressView.progressTintColor = UIColor.blueColor() //走过进度条颜色
progressView.trackTintColor = UIColor.greenColor() //未走进度条颜色
self.view.addSubview(progressView)
}
func btnOnclick(){
button.enabled = false
timer = NSTimer.scheduledTimerWithTimeInterval(, target: self, selector:"timerAction", userInfo: nil, repeats: true)
timer.fire()
}
func timerAction(){
progressView.progress += 0.02
if progressView.progress == {
progressView.setProgress(, animated: true)
}
} override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
} }
UIAlertController
//
// ViewController.swift
// UIAlertController
//
// Created by shaoting on 16/3/25.
// Copyright © 2016年 9elephas. All rights reserved.
// swift控件学习篇
// alert //
// import UIKit class ViewController: UIViewController { var alert1:UIAlertController!
var alert2:UIAlertController!
var actionSheet:UIAlertController! var cancelAction = UIAlertAction!()
var okAction = UIAlertAction!()
var deleteAction = UIAlertAction!() override func viewDidLoad() {
super.viewDidLoad()
// 定义一个按钮,用于点击显示最简单的Alert
let button1 = UIButton(type: UIButtonType.System)
button1.frame = CGRect(x: , y: , width: , height: )
button1.setTitle("最简单的Alert", forState: UIControlState.Normal)
button1.addTarget(self, action: Selector("action1"), forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(button1)
// 定义一个按钮,用于点击显示带文本输入框的Alert
let button2:UIButton = UIButton(type: UIButtonType.System)
button2.frame = CGRect(x: , y: , width: , height: )
button2.setTitle("带文本输入框的Alert", forState: UIControlState.Normal)
button2.addTarget(self, action: Selector("action2"), forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(button2)
// 定义一个按钮,用于点击显示上拉菜单
let button3 = UIButton(type: UIButtonType.System)
button3.frame = CGRect(x: , y: , width: , height: )
button3.setTitle("上拉菜单", forState: UIControlState.Normal)
button3.addTarget(self, action: Selector("action3"), forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(button3) // Do any additional setup after loading the view, typically from a nib.
} func action1(){
//定义菜单按钮
cancelAction = UIAlertAction(title: "取消", style: UIAlertActionStyle.Cancel, handler: nil)
okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: { (UIAlertAction) -> Void in
print("you choose OK")
})
deleteAction = UIAlertAction(title: "删除", style: UIAlertActionStyle.Destructive, handler: { (UIAlertAction) -> Void in
print("you choose delete")
})
//定义一个按钮,显示带文本框的Alert
alert1 = UIAlertController(title: "最简单的Alert", message: "this is a simple", preferredStyle: UIAlertControllerStyle.Alert)
alert1.addAction(cancelAction)
alert1.addAction(okAction)
alert1.addAction(deleteAction)
self.presentViewController(alert1, animated: true, completion: nil)
}
func action2(){
alert2 = UIAlertController(title: "带输入框的alert", message: "this is a test alert", preferredStyle: UIAlertControllerStyle.Alert)
alert2.addTextFieldWithConfigurationHandler {(textFiled:UITextField!) -> Void in
textFiled.placeholder = "username"
}
alert2.addTextFieldWithConfigurationHandler { (textFiled:UITextField) -> Void in
textFiled.placeholder = "password"
textFiled.secureTextEntry = true
}
let loginAction = UIAlertAction(title: "login", style: UIAlertActionStyle.Default) { (action:UIAlertAction!) -> Void in
let name = (self.alert2.textFields?.first!)! as UITextField
let password = (self.alert2.textFields?.last)! as UITextField
print("name:\(name.text);password:\(password.text)")
}
alert2.addAction(loginAction)
self.presentViewController(alert2, animated: true, completion: nil)
}
func action3(){
actionSheet = UIAlertController(title: "上拉菜单", message: "this is a action", preferredStyle: UIAlertControllerStyle.ActionSheet)
cancelAction = UIAlertAction(title: "取消", style: UIAlertActionStyle.Cancel, handler: nil)
okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: { (UIAlertAction) -> Void in
print("you choose OK")
})
deleteAction = UIAlertAction(title: "删除", style: UIAlertActionStyle.Destructive, handler: { (UIAlertAction) -> Void in
print("you choose delete")
})
actionSheet.addAction(okAction)
actionSheet.addAction(cancelAction)
actionSheet.addAction(deleteAction)
self.presentViewController(actionSheet, animated: true, completion: nil) } override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
} }
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
学习网站: