類似於,我們加載網頁時候的進度條,我們來看看它們是怎麼工作的。
#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更了,明天繼續。求知若飢,虛心若愚。這幾天找工作找的蠻辛苦的,在大上海不容易啊,明天就要去面試了,估計一天都在外面跑,只有晚上有時間能給大家寫東西,我會保持下去的,同時保證質量,當然如果有錯誤的話,大家可以明確指出來,我可以修改。嘻嘻,祝大家美夢,晚安。