实现类似读取数据进度条效果
代码如下:
①创建UIProgressView对象
1)定义属性
{
NSTimer *_proTimer;//计时
}
@property (nonatomic, retain) UIProgressView *proView;
//显示进度信息
@property (nonatomic, retain) UILabel *proLabel;
@property (nonatomic, assign) float proValue;//保存进度值
//创建控件UIProgressView
self.proView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault];
//设置frame
self.proView.frame = CGRectMake(10, 260, 300, 100);
//设置当前进度当前值
self.proView.progress = 0;
// self.proValue = 0.4;
//设置进度条的颜色
self.proView.progressTintColor = [UIColor orangeColor];
//导轨的颜色
self.proView.trackTintColor = [UIColor greenColor];
//修改进度条的高度(仿射变换)
self.proView.transform = CGAffineTransformMakeScale(1.0f,15.0f);
//添加到当前的试图上
[self.view addSubview:self.proView];
//释放
[self.proView release];
②设置Label显示进度值,
//创建显示进度信息的label
self.proLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 300, 120, 30)];
self.proLabel.backgroundColor = [UIColor clearColor];
//显示当前进度值
self.proLabel.text = [NSString stringWithFormat:@"loading%1.f%%",self.proView.progress*100];
[self.view addSubview:self.proLabel];
[self.proLabel release];
③创建计时器,平处理事件
//通过计时器循环改进度值
_proTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(changeProgress) userInfo:nil repeats:YES];
//处理事件
- (void)changeProgress {
//设置进度每次变化值
self.proValue += 0.05;
self.proView.progress = self.proValue;
//显示实时的进度值
self.proLabel.text = [NSString stringWithFormat:@"loading%1.f%%",self.proValue * 100];
if (self.proValue >= 1) {
self.proValue = 0;
self.proLabel.text = @"进度完成";
//停止计数器
[_proTimer invalidate];
}
}
原文:http://blog.csdn.net/zfx5130/article/details/43538323