1,创建进度条
1
2
3
4
|
var
progressView=
UIProgressView
(progressViewStyle:
UIProgressViewStyle
.
Default
)
progressView.center=
self
.view.center
progressView.progress=0.5
//默认进度50%
.view.addSubview(progressView);
|
progressView.setProgress(0.8,animated:
true
)
|
progressView.progressTintColor=
UIColor
.greenColor()
//已有进度颜色
progressView.trackTintColor=
.blueColor()
//剩余进度颜色(即进度槽颜色)
|
步骤如下:
一、在函数外部定义三个变量
var timer: NSTimer!
var remainTime = 0
var progress: UIProgressView!
overridefuncviewDidLoad() {
//这里放置步骤二的代码即可
}
二、在函数中创建进度条控件
progress = UIProgressView(frame: CGRect(x: (width-100)/2,y: height/2,width: 100,height: 1))
progress.progress = 0
progress.progressTintColor = UIColor.redColor()
progress.trackTintColor = UIColor.blackColor()
self.view.addSubview(progress)
timer = NSTimer.scheduledTimerWithTimeInterval(1,target: self,selector: "timerAction",userInfo: nil,repeats:true)
timer.fire()
三、创建事件响应的函数
func timerAction() {
if(remainTime >= 100){
timer.invalidate()
var homeView = UIStoryboard(name: "Main",bundle:nil).instantiateViewControllerWithIdentifier("HomeViewController") as! HomeViewController
self.presentViewController(homeView,animated: true,completion: nil)
} else {
remainTime = remainTime + 35
let progressValue = Float(remainTime)/100
progress.setProgress(progressValue,animated:true)
}
运行后,就可以看到进度条的加载效果了。