1.創(chuàng)建定時器
//創(chuàng)建Timer
self.timer=dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER,0,0,dispatch_get_main_queue());
//設(shè)置定時器的觸發(fā)時間(1秒后)和時間間隔(每隔2秒)
dispatch_source_set_timer(self.timer,dispatch_time(DISPATCH_TIME_NOW,1*NSEC_PER_SEC),2*NSEC_PER_SEC,0);
//設(shè)置回調(diào)
dispatch_source_set_event_handler(self.timer, ^{
NSLog(@"Timer %@", [NSThreadcurrentThread]);
});
//開始定時器
dispatch_resume(self.timer);
2.取消定時器
dispatch_cancel(self.timer);
self.timer=nil;
注意:GCD定時器不用加入RunLoop
Dome: