GCD定时器

//获得队列

dispatch_queue_t queue = dispatch_get_global_queue(0,0);

//创建一个定时器

self.time = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER,0,0, queue);

//设置开始时间

dispatch_time_t start = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0* NSEC_PER_SEC));

//设置时间间隔

uint64_t interval = (uint64_t)(2.0* NSEC_PER_SEC);

//设置定时器

dispatch_source_set_timer(self.time, start, interval,0)

;//设置回调

dispatch_source_set_event_handler(self.time, ^{

//设置当执行五次是取消定时器

count++;if(count ==5){

dispatch_cancel(self.time);

}

});

//由于定时器默认是暂停的所以我们启动一下

//启动定时器

dispatch_resume(self.time);

//暂停

dispatch_suspend(self.timerG);

//播放

dispatch_resume(self.timerG);

//销毁

dispatch_source_cancel(self.timerG);

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • /** 定时器(这里不用带*,因为dispatch_source_t就是个类,内部已经包含了*) */ @prop...
    CocoaJason阅读 4,101评论 0 0
  • GCD定时器不受RunLoop影响,比NSTimer更精确 //获得队列 //dispatch_queue_t q...
    rlqs阅读 3,255评论 0 0
  • 提到定时器,NStimer肯定是我们最为熟悉的。 但是NStimer有着很大的缺点,并不准确。 通俗点说,就是它该...
    mengyingguo阅读 2,910评论 0 0
  • 定时器分为三种:1、NSTimer 2、CADisplayLink 3、GCD实现 今天着重学习一下GCD中...
    这个夏天有点冷阅读 4,569评论 0 0
  • #import"ViewController.h" NSInteger count=0; @interfaceVi...
    清蘂翅膀的技术阅读 4,189评论 0 0

友情链接更多精彩内容