標籤:ios ios開發 線程 多線程 編程
類似於,我們載入網頁時候的進度條,我們來看看它們是怎麼工作的。
#import "ViewController.h"
@interface ViewController ()
{
UIProgressView *_view;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
//設定進度條
_view = [[UIProgressViewalloc]init];
_view.frame =CGRectMake(20,100, 300,3);
_view.progress =0;
[self.viewaddSubview:_view];
UIButton *btn = [[UIButtonalloc]init];
btn.frame =CGRectMake(60,150, 200,20);
[btn setTitle:@"點擊更新"forState:UIControlStateNormal];
btn.backgroundColor = [UIColorredColor];
[btn addTarget:selfaction:@selector(btnClick:)forControlEvents:UIControlEventTouchUpInside];
[self.viewaddSubview:btn];
}
//在子線程中更新進度條
-(void)btnClick:(UIButton *)btn
{
//表現在幕後
[selfperformSelectorInBackground:@selector(updateProgressView)withObject:nil];
}
-(void)updateProgressView
{
while (1) {
if (_view.progress<1) {
//每次累加0.0001的值
NSNumber *number = [NSNumbernumberWithFloat:_view.progress+0.0001];
[selfperformSelectorOnMainThread:@selector(setProgressViewProgress:)withObject:number waitUntilDone:NO];
//延遲0.0005
[NSThreadsleepForTimeInterval:0.0005];
}
else//否則結束
{
break;
}
}
NSLog(@"1111");
}
//更新進度條()
-(void)setProgressViewProgress:(NSNumber *)number
{
//給progressview設定數值
[_viewsetProgress:[number floatValue]];
}
@end
今天2更了,明天繼續。求知若饑,虛心若愚。這幾天找工作找的蠻辛苦的,在大上海不容易啊,明天就要去面試了,估計一天都在外面跑,只有晚上有時間能給大家寫東西,我會保持下去的,同時保證品質,當然如果有錯誤的話,大家可以明確指出來,我可以修改。嘻嘻,祝大家美夢,晚安。
ios線程練習更新進度條