(十)iOS开发之多线程—GCD定时器

2017-10-28  本文已影响74人  笃Boy

1.创建定时器

//创建Timer

self.timer=dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER,0,0,dispatch_get_main_queue());

//设置定时器的触发时间(1秒后)和时间间隔(每隔2秒)

dispatch_source_set_timer(self.timer,dispatch_time(DISPATCH_TIME_NOW,1*NSEC_PER_SEC),2*NSEC_PER_SEC,0);

//设置回调

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:

https://github.com/tangbinbinM/iOS-GCD-.git

上一篇 下一篇

猜你喜欢

热点阅读