您的位置:首页 > 移动开发 > IOS开发

GCD定时器

2016-04-15 10:49 330 查看
// 比NSTimer更精准,不受RunLoop的Model影响
int count = 0;
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
// 获得队列
dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
// 1 创建一个定时器
self.timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
//    dispatch_time_t start = dispatch_time(DISPATCH_TIME_NOW, 3.0 * NSEC_PER_SEC);
//    dispatch_time_t interval = 2.0 * NSEC_PER_SEC;
dispatch_source_set_timer(self.timer, DISPATCH_TIME_NOW, 2.0 * NSEC_PER_SEC, 0.0 * NSEC_PER_SEC);
dispatch_source_set_event_handler(self.timer, ^{
NSLog(@"------%@", [NSThread currentThread]);
count++;
if (count == 4) {
dispatch_cancel(self.timer);
self.timer = nil;
}
});
dispatch_resume(self.timer);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  iOS GCD