在GCD中使用顺序队列
@property(nonatomic,retain)dispatch_queue_t serialQueue;
- (void)viewDidLoad {
[super viewDidLoad];
self.serialQueue=dispatch_queue_create(DISPATCH_QUEUE_SERIAL, 0);
...
-(void)bigTaskAction{
dispatch_async(self.serialQueue, ^{
dispatch_sync(dispatch_get_main_queue(), ^{
[self.myActivityIndicator startAnimating];
});
int updateUIWhen=1000;
for (int i=0;i<10000;i++) {
NSString*newString=[NSString stringWithFormat:@"i=%i",i ];
NSLog(@"%@ ",newString);
if (i==updateUIWhen) {
float f =(float)i/10000;
NSNumber*percentDone=[NSNumber numberWithFloat:f];
dispatch_sync(dispatch_get_main_queue(), ^{
[self.myProcessView setProgress:[percentDone floatValue ]animated:YES];
});
updateUIWhen=updateUIWhen+500;
}
}
dispatch_sync(dispatch_get_main_queue(), ^{
[self.myProcessView setProgress:1.0 animated:YES];
[self.myActivityIndicator stopAnimating];
});
});
}
现在只需要button addtarget 剩下的只需要这一个方法就能实现功能
网友评论