// 进度条 UIProgressView 顾名思义用来显示进度的,如音乐,视频的播放进度,和文件的上传下载进度等
import UIKit
class ViewController: UIViewController {
var myProgressView:UIProgressView!
var timer:NSTimer!
var proValue:Double!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
/**
1,创建进度条
*/
let progressView = UIProgressView(progressViewStyle:UIProgressViewStyle.Default)
progressView.center = self.view.center
// 设置进度条进度(0.0-1.0之间,默认为0.0)
progressView.progress = 0.0
self.view.addSubview(progressView);
/**
2,设置进度,同时有动画效果
*/
progressView.setProgress(0.8, animated: true)
/**
3,改变进度条的颜色
*/
// 设置已走过的进度条颜色
progressView.progressTintColor = UIColor.greenColor()
// 设置未走过进度的进度条颜色
progressView.trackTintColor = UIColor.blueColor()
/**
4,使用计时器和按钮让进度条做动画
*/
myProgressView = UIProgressView(frame:CGRectMake(100, 50, 150, 20))
myProgressView.progressViewStyle = .Default
self.view.addSubview(myProgressView);
// 按钮
let button = UIButton(type:.Custom)
button.frame = CGRectMake(10, 20, 60, 60)
button.setTitle("点我", forState: .Normal)
button.backgroundColor = UIColor.blackColor()
button.addTarget(self, action: #selector(buttonAction(_:)), forControlEvents: .TouchUpInside)
self.view.addSubview(button)
}
// 按钮响应事件
func buttonAction(sender:UIButton) {
proValue = 0;
//利用计时器,每隔1秒调用一次(changeProgress)
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: #selector(changeProgress), userInfo: nil, repeats: true)
}
// 计时器响应事件
func changeProgress() {
proValue = proValue + 1.0 // 改变ProValue的值
if proValue > 5 {
// 停止使用计时器
print("停止使用计时器")
timer.invalidate()
} else {
myProgressView.setProgress((Float)(proValue/5), animated: true) // 重置进度条
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Swift 进度条 UIProgressView
最新推荐文章于 2024-08-29 07:47:17 发布