步骤如下:
一、在函数外部定义三个变量
var timer: NSTimer!
var remainTime = 0
var progress: UIProgressView!
override func viewDidLoad() {
//这里放置步骤二的代码即可
}
二、在函数中创建进度条控件
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)
}
}
运行后,就可以看到进度条的加载效果了。