iOS dispatch_source_timer 分享demo
DISPATCH_SOURCE_TYPE_TIMER
另外一种gcd封装好的计时器处理工具,上一小段使用demo代码
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
{
UIProgressView*_progressView;
dispatch_source_t _timerSource;
CGFloat progressTotal;
}
- (void)viewDidLoad
{
[super viewDidLoad];
progressTotal = 0;
[self setupUI];
}
- (void)setupUI
{
_progressView = [[UIProgressView alloc]initWithProgressViewStyle:UIProgressViewStyleDefault];
_progressView.progressTintColor = [UIColor blueColor];
_progressView.trackTintColor = [UIColor redColor];
[_progressView setProgress:0];
_progressView.frame = CGRectMake(10, 100, self.view.frame.size.width - 20, 5);
_progressView.transform = CGAffineTransformMakeScale(1.0f,3.0f);
[self.view addSubview:_progressView];
UIButton*button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundColor:[UIColor blackColor]];
[button setTitle:@"start" forState:UIControlStateNormal];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[self.view addSubview:button];
button.frame = CGRectMake(180, 160, 50, 30);
[button addTarget:self action:@selector(gcdResourceTest) forControlEvents:UIControlEventTouchUpInside];
}
- (void)gcdResourceTest
{
dispatch_queue_t timerQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);
_timerSource = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, timerQueue);
double interval = 1 * NSEC_PER_SEC;
dispatch_source_set_timer(_timerSource, dispatch_time(DISPATCH_TIME_NOW, 0), interval, 0);
dispatch_block_t executingBlock = ^{
NSLog(@"progress:%.2f",progressTotal);
progressTotal += 0.1;
dispatch_async(dispatch_get_main_queue(), ^{
[_progressView setProgress:progressTotal animated:YES];
});
if(progressTotal>=1.0)
{
dispatch_source_cancel(_timerSource);
}
};
dispatch_source_set_event_handler(_timerSource, [executingBlock copy]);
dispatch_source_set_cancel_handler(_timerSource, ^{
NSLog(@"process canceled");
});
dispatch_resume(_timerSource);
}
@end
输出:
2016-03-29 18:27:41.858 DispatchSourceTestOne[36439:255867] progress:0.00 2016-03-29 18:27:42.858 DispatchSourceTestOne[36439:255867] progress:0.10 2016-03-29 18:27:43.859 DispatchSourceTestOne[36439:255867] progress:0.20 2016-03-29 18:27:44.860 DispatchSourceTestOne[36439:255867] progress:0.30 2016-03-29 18:27:45.859 DispatchSourceTestOne[36439:255867] progress:0.40 2016-03-29 18:27:46.858 DispatchSourceTestOne[36439:255867] progress:0.50 2016-03-29 18:27:47.858 DispatchSourceTestOne[36439:255867] progress:0.60 2016-03-29 18:27:48.861 DispatchSourceTestOne[36439:255867] progress:0.70 2016-03-29 18:27:49.862 DispatchSourceTestOne[36439:255867] progress:0.80 2016-03-29 18:27:50.860 DispatchSourceTestOne[36439:255867] progress:0.90 2016-03-29 18:27:51.857 DispatchSourceTestOne[36439:255867] progress:1.00 2016-03-29 18:27:51.858 DispatchSourceTestOne[36439:255867] process canceled
1秒调用一次,当进度大于等于1timer停止,cancel block回调
这里注意dispatch_source_t timerSource是一个实例变量,如果放在函数里面创建,局部变量内存释放比较早,会没有效果。
浙公网安备 33010602011771号