///GCD 去实现NSTimer
///1 取得一个队列
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
///2 创建一个timer 放队列里面
dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
///3 设置timer执行的时间,执行的时间间隔,精确度
dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, 2* NSEC_PER_SEC, 0.1*NSEC_PER_SEC);
self.timer = timer;
///4 设置timer执行事件
__weak typeof(self) weakSelf = self;
dispatch_source_set_event_handler(timer, ^{
[weakSelf doSomething];
});
///5 激活timer
dispatch_resume(timer);
///6 取消timer
//dispatch_source_cancel(timer);
网友评论