美文网首页
(十)iOS开发之多线程—GCD定时器

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

作者: 笃Boy | 来源:发表于2017-10-28 15:51 被阅读74次

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

相关文章

网友评论

      本文标题:(十)iOS开发之多线程—GCD定时器

      本文链接:https://www.haomeiwen.com/subject/fvdspxtx.html