CGRunLoopTimerRef是基于時(shí)間的觸發(fā)器,基本上說屯换,就是NSTimer邀泉,他受到RunLoop的Mode影響,所以有點(diǎn)時(shí)候丰泊,我們說NSTimer可能會(huì)不準(zhǔn)
但是CGD的定時(shí)器不受到RunLoop的影響
首先創(chuàng)建一個(gè)定時(shí)器
/** timer 本身就是一個(gè)對(duì)象,所以不要用*修飾,必須對(duì)他強(qiáng)引用始绍,否則修釋放*/
@property(strong,nonatomic) dispatch_source_t timer;
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//創(chuàng)建一個(gè)定時(shí)器
dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
self.timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
// dispatch_time_t start = dispatch_time(DISPATCH_TIME_NOW, 1.0 * NSEC_PER_SEC);
NSLog(@"main -%@",[NSThread currentThread]);
dispatch_source_set_timer(self.timer, DISPATCH_TIME_NOW, 2 * NSEC_PER_SEC, 0 * NSEC_PER_SEC);
dispatch_source_set_event_handler(self.timer, ^{
NSLog(@"%@",[NSThread currentThread]);
});
dispatch_resume(self.timer);
}
數(shù)據(jù)打印:每隔兩秒打印一次瞳购,非常準(zhǔn)時(shí)
2017-04-20 10:48:42.129 WXAllTest[1274:26444] main -<NSThread: 0x60800007c640>{number = 1, name = main}
2017-04-20 10:48:42.130 WXAllTest[1274:26492] <NSThread: 0x60000026f480>{number = 3, name = (null)}
2017-04-20 10:48:44.131 WXAllTest[1274:26492] <NSThread: 0x60000026f480>{number = 3, name = (null)}
2017-04-20 10:48:46.130 WXAllTest[1274:26492] <NSThread: 0x60000026f480>{number = 3, name = (null)}
2017-04-20 10:48:48.130 WXAllTest[1274:26492] <NSThread: 0x60000026f480>{number = 3, name = (null)}
2017-04-20 10:48:50.130 WXAllTest[1274:26492] <NSThread: 0x60000026f480>{number = 3, name = (null)}
2017-04-20 10:48:52.131 WXAllTest[1274:26492] <NSThread: 0x60000026f480>{number = 3, name = (null)}
2017-04-20 10:48:54.130 WXAllTest[1274:26492] <NSThread: 0x60000026f480>{number = 3, name = (null)}
2017-04-20 10:48:56.131 WXAllTest[1274:26492] <NSThread: 0x60000026f480>{number = 3, name = (null)}
2017-04-20 10:48:58.130 WXAllTest[1274:26492] <NSThread: 0x60000026f480>{number = 3, name = (null)}
2017-04-20 10:49:00.130 WXAllTest[1274:26492] <NSThread: 0x60000026f480>{number = 3, name = (null)}
如果想3秒鐘之后再去啟動(dòng)定時(shí)器,每隔2s執(zhí)行某件事怎么辦亏推?
dispatch_source_set_timer(self.timer, DISPATCH_TIME_NOW, 2 * NSEC_PER_SEC, 0 * NSEC_PER_SEC);
第一個(gè)參數(shù)表示timer学赛,第二個(gè)是開始時(shí)間,默認(rèn)是當(dāng)前時(shí)間DISPATCH_TIME_NOW
,第三個(gè)參數(shù)是每隔幾秒吞杭,單位是納秒;
我們只要對(duì)第二個(gè)參數(shù)從新處理下就行盏浇,但是這個(gè)比較特殊,應(yīng)該這樣構(gòu)建,否則無效
dispatch_time_t start = dispatch_time(DISPATCH_TIME_NOW, 1.0 * NSEC_PER_SEC);
停止一個(gè)定時(shí)器
//如何停止CGD定時(shí)器芽狗?
static int count = 0;
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//創(chuàng)建一個(gè)定時(shí)器
dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
self.timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
NSLog(@"main -%@",[NSThread currentThread]);
//設(shè)置一下timer的開始時(shí)間和間隔
dispatch_source_set_timer(self.timer, DISPATCH_TIME_NOW, 2 * NSEC_PER_SEC, 0 * NSEC_PER_SEC);
//timer每次執(zhí)行的事情
dispatch_source_set_event_handler(self.timer, ^{
NSLog(@"%@",[NSThread currentThread]);
if (count == 4) {
dispatch_cancel(self.timer);
self.timer = nil;
}
count ++;
});
//啟動(dòng)timer绢掰,否則不能啟動(dòng)
dispatch_resume(self.timer);
}
2017-04-20 11:00:14.659 WXAllTest[1501:38869] main -<NSThread: 0x60800007d0c0>{number = 1, name = main}
2017-04-20 11:00:14.660 WXAllTest[1501:39450] <NSThread: 0x600000272540>{number = 3, name = (null)}
2017-04-20 11:00:16.660 WXAllTest[1501:39450] <NSThread: 0x600000272540>{number = 3, name = (null)}
2017-04-20 11:00:18.660 WXAllTest[1501:39450] <NSThread: 0x600000272540>{number = 3, name = (null)}
2017-04-20 11:00:20.661 WXAllTest[1501:39450] <NSThread: 0x600000272540>{number = 3, name = (null)}
2017-04-20 11:00:22.660 WXAllTest[1501:39450] <NSThread: 0x600000272540>{number = 3, name = (null)}
最后只打印了這些,說明關(guān)閉了