类似于,我们加载网页时候的进度条,我们来看看它们是怎么工作的。
#import "ViewController.h"
@interface ViewController ()
{
UIProgressView *_view;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super
viewDidLoad];
//设置进度条
_view = [[UIProgressView
alloc]init];
_view.frame =
CGRectMake(20,
100, 300,
3);
_view.progress =
0;
[self.view
addSubview:_view];
UIButton *btn = [[UIButton
alloc]init];
btn.frame =
CGRectMake(60,
150, 200,
20);
[btn setTitle:@"点击更新"
forState:UIControlStateNormal];
btn.backgroundColor = [UIColor
redColor];
[btn addTarget:self
action:@selector(btnClick:)
forControlEvents:UIControlEventTouchUpInside];
[self.view
addSubview:btn];
}
//在子线程中更新进度条
-(void)btnClick:(UIButton *)btn
{
//表现在幕后
[self
performSelectorInBackground:@selector(updateProgressView)
withObject:nil];
}
-(void)updateProgressView
{
while (1) {
if (_view.progress<1) {
//每次累加0.0001的值
NSNumber *number = [NSNumber
numberWithFloat:_view.progress+0.0001];
[self
performSelectorOnMainThread:@selector(setProgressViewProgress:)
withObject:number waitUntilDone:NO];
//延迟0.0005
[NSThread
sleepForTimeInterval:0.0005];
}
else//否则结束
{
break;
}
}
NSLog(@"1111");
}
//更新进度条()
-(void)setProgressViewProgress:(NSNumber *)number
{
//给progressview设置数值
[_view
setProgress:[number floatValue]];
}
@end
今天2更了,明天继续。求知若饥,虚心若愚。这几天找工作找的蛮辛苦的,在大上海不容易啊,明天就要去面试了,估计一天都在外面跑,只有晚上有时间能给大家写东西,我会保持下去的,同时保证质量,当然如果有错误的话,大家可以明确指出来,我可以修改。嘻嘻,祝大家美梦,晚安。