您的位置:首页 > 产品设计 > UI/UE

IOS--GCD修改主线程UI属性

2016-03-02 15:24 423 查看
TestController.m

#import "TestController.h"
#import "TestView.h"

@interface TestController()

@property(nonatomic,strong)UIButton *button;
@property(nonatomic,strong)UIProgressView *progress;

@end

@implementation TestController

- (void)viewDidLoad
{
[super viewDidLoad];

_button = [UIButton buttonWithType:UIButtonTypeSystem];

_button.frame = CGRectMake(0, 20, 100, 20);
[_button setTitle:@"Hello" forState:UIControlStateNormal];

[_button addTarget:self action:@selector(start:) forControlEvents:UIControlEventTouchUpInside];

_progress = [[UIProgressView alloc]initWithFrame:CGRectMake(20, 60, 240, 25)];

[self.view addSubview:_button];
[self.view addSubview:_progress];
}

//GCD
-(void)start:(UIButton*)sender
{
dispatch_queue_t queeue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queeue, ^{
[self thLoop];
});
}

-(void)thLoop
{
float i;
while (i<1) {
i+=0.01;
[NSThread sleepForTimeInterval:0.1];
//修改主线程UI属性,需要调用dispatch_get_main_queue()函数
dispatch_async(dispatch_get_main_queue(), ^{
self.progress.progress = i;
});
}

}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: