1.聲明控件
進度條有兩種樣式
- bar
- default
兩種樣式使用效果如下:
self.view.backgroundColor = UIColor.red
progressView = UIProgressView(progressViewStyle: .default)
progressView.frame = CGRect(x: 0, y: 0, width: 200, height: 10)
progressView.layer.position = CGPoint(x: self.view.frame.width/2, y: 90)
progressView.progress = 0.5
self.view.addSubview(progressView)
progressView2 = UIProgressView(progressViewStyle: .bar)
progressView2.frame = CGRect(x: 0, y: 0, width: 200, height: 20)
progressView2.layer.position = CGPoint(x: self.view.frame.width/2, y: 110)
progressView2.progress = 0.5
self.view.addSubview(progressView2)
運行效果:
2.啟用進度條加載動畫
progressView.setProgress(0.5, animated: true)
3.改變進度條顏色
progressView.progressTintColor = UIColor.green //進度顏色
progressView.trackTintColor = UIColor.yellow //剩余進度顏色
4.設置progressView的高度
其實細心的小伙伴應該已經(jīng)發(fā)現(xiàn)片挂,在聲明控件的時候,第一個height:10贞盯,第二個heigth:20音念,從截圖上來看兩個的高度并沒有差異,所以通過設置progressView的高度并不能達到目的躏敢,不過我們可以通過改變 progressView 的 scale(縮放比例)來實現(xiàn)高度的變化
progressView = UIProgressView(progressViewStyle: .default)
progressView.frame = CGRect(x: 0, y: 0, width: 200, height: 10)
progressView.layer.position = CGPoint(x: self.view.frame.width/2, y: 90)
progressView.setProgress(0.5, animated: true)
progressView.progressTintColor = UIColor.green //進度顏色
progressView.trackTintColor = UIColor.yellow //剩余進度顏色
//通過改變進度條高度(寬度不變闷愤,高度變?yōu)槟J的2倍)
progressView.transform = CGAffineTransform(scaleX: 1.0, y: 2.0)
self.view.addSubview(progressView)
progressView2 = UIProgressView(progressViewStyle: .bar)
progressView2.frame = CGRect(x: 0, y: 0, width: 200, height: 20)
progressView2.layer.position = CGPoint(x: self.view.frame.width/2, y: 110)
progressView2.progress = 0.5
self.view.addSubview(progressView2)
5.其它
推薦一個自定義的進度條樣式:http://www.code4app.com/thread-6304-1-1.html 是用OC實現(xiàn)的,在使用的過程中可能大伙還得先熟悉一下swift中使用oc控件