IOS--GCD修改主线程UI属性
TestController.m
1 #import "TestController.h" 2 #import "TestView.h" 3 4 @interface TestController() 5 6 @property(nonatomic,strong)UIButton *button; 7 @property(nonatomic,strong)UIProgressView *progress; 8 9 @end 10 11 @implementation TestController 12 13 - (void)viewDidLoad 14 { 15 [super viewDidLoad]; 16 17 _button = [UIButton buttonWithType:UIButtonTypeSystem]; 18 19 _button.frame = CGRectMake(0, 20, 100, 20); 20 [_button setTitle:@"Hello" forState:UIControlStateNormal]; 21 22 [_button addTarget:self action:@selector(start:) forControlEvents:UIControlEventTouchUpInside]; 23 24 _progress = [[UIProgressView alloc]initWithFrame:CGRectMake(20, 60, 240, 25)]; 25 26 [self.view addSubview:_button]; 27 [self.view addSubview:_progress]; 28 } 29 30 //GCD 31 -(void)start:(UIButton*)sender 32 { 33 dispatch_queue_t queeue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); 34 dispatch_async(queeue, ^{ 35 [self thLoop]; 36 }); 37 } 38 39 -(void)thLoop 40 { 41 float i; 42 while (i<1) { 43 i+=0.01; 44 [NSThread sleepForTimeInterval:0.1]; 45 //修改主线程UI属性,需要调用dispatch_get_main_queue()函数 46 dispatch_async(dispatch_get_main_queue(), ^{ 47 self.progress.progress = i; 48 }); 49 } 50 51 } 52 53 54 @end

浙公网安备 33010602011771号