// 比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);
}
GCD定时器
最新推荐文章于 2022-07-15 14:09:21 发布