UIProgressView

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52

#import "ViewController.h"

@interface ViewController ()
{
NSTimer *_timer;
}
@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
// 初始化UIProgressView并设置frame
// UIProgressView 高度是固定的
UIProgressView *progressView = [[UIProgressView alloc]initWithFrame:CGRectMake(100, 100, 200, 100)];
// 设置进度
// 0.0 .. 1.0, default is 0.0.
progressView.progress = 0.25;
// 已加载进度的颜色
progressView.progressTintColor = [UIColor redColor];
// 未加载进度的颜色
progressView.trackTintColor = [UIColor greenColor];
// 设置图片会覆盖上面设置的颜色
// 已加载进度的图片
progressView.progressImage = [UIImage imageNamed:@"1"];
// 未加载进度的图片
progressView.trackImage = [UIImage imageNamed:@"2"];

// 设置tag值
progressView.tag = 10;

// 添加到视图
[self.view addSubview:progressView];

// 初始化timer
_timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(timerRun) userInfo:nil repeats:YES];

}
-(void)timerRun{
UIProgressView *progressView = (UIProgressView *)[self.view viewWithTag:10];

if (progressView.progress < 1.0) {
progressView.progress += 0.01;
}else{
NSLog(@"下载完成");
// 使计时器失效
[_timer invalidate];
}

}
@end