1,创建进度条
1
2
3
4
|
var progressView= UIProgressView (progressViewStyle: UIProgressViewStyle . Default ) progressView.center= self .view.center progressView.progress=0.5 //默认进度50% self .view.addSubview(progressView); |
2,设置进度,同时有动画效果
1
|
progressView.setProgress(0.8,animated: true ) |
3,改变进度条颜色
1
2
|
progressView.progressTintColor= UIColor .greenColor() //已有进度颜色 progressView.trackTintColor= UIColor .blueColor() //剩余进度颜色(即进度槽颜色) |
4,设置progressView的宽度(进度条长度)
通常情况下,我们可以在初始化 progressView 的时候通过 frame 属性设置其宽度(进度条长度)。
比如下面样例,我在屏幕中放置一个横向宽度是200的进度条,其位置是水平居中。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
import UIKit class ViewController : UIViewController { override func viewDidLoad() { super .viewDidLoad() //将背景设为黑色 self .view.backgroundColor = UIColor .blackColor() //创建一个宽度是200的进度条 let myProgressView = UIProgressView (frame: CGRectMake (0, 0, 200, 10)) //设置进度条位置(水平居中) myProgressView.layer.position = CGPoint (x: self .view.frame.width/2, y: 100) //进度条条进度 myProgressView.progress = 0.3 //把进度条添加到view中来 self .view.addSubview(myProgressView) } override func didReceiveMemoryWarning() { super .didReceiveMemoryWarning() } } |
2,设置progressView的高度
但我们会发现无论如何设置 progressView 的高度,其最终显示出来的高度都不会变化。所以如果想改变高度,可以换个思路,通过改变 progressView 的 scale(缩放比例)来实现。
下面样例将进度条高度调整到默认的5倍。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
import UIKit class ViewController : UIViewController { override func viewDidLoad() { super .viewDidLoad() //将背景设为黑色 self .view.backgroundColor = UIColor .blackColor() //创建一个宽度是200的进度条 let myProgressView = UIProgressView (frame: CGRectMake (0, 0, 200, 10)) //设置进度条位置(水平居中) myProgressView.layer.position = CGPoint (x: self .view.frame.width/2, y: 100) //通过变形改变进度条高度( 横向宽度不变,纵向高度变成默认的5倍) myProgressView.transform = CGAffineTransformMakeScale (1.0, 5.0) //进度条条进度 myProgressView.progress = 0.3 //把进度条添加到view中来 self .view.addSubview(myProgressView) } override func didReceiveMemoryWarning() { super .didReceiveMemoryWarning() }
|