1
?創(chuàng)建一個(gè)定時(shí)器(dispatch_source_t本質(zhì)還是個(gè)OC對(duì)象)
/** 定時(shí)器(這里不用帶*炼邀,因?yàn)閐ispatch_source_t就是個(gè)類奸腺,內(nèi)部已經(jīng)包含了*) */
@property (nonatomic, strong) dispatch_source_t timer;
self.timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
第一個(gè)參數(shù)是類型
第四個(gè)是隊(duì)列 (自己創(chuàng)建主隊(duì)列 dispatch_queue_t queue = dispatch_get_main_queue(); 或者??dispatch_queue_t queue = dispatch_get_global_queue(0, 0);)
?// 設(shè)置定時(shí)器的各種屬性(幾時(shí)開(kāi)始任務(wù)彰触,每隔多長(zhǎng)時(shí)間執(zhí)行一次)
? ? // GCD的時(shí)間參數(shù),一般是納秒(1秒 == 10的9次方納秒)
? ? // 何時(shí)開(kāi)始執(zhí)行第一個(gè)任務(wù)
? ? // dispatch_time(DISPATCH_TIME_NOW, 1.0 * NSEC_PER_SEC) 比當(dāng)前時(shí)間晚3秒
? ? dispatch_time_t start = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC));
? ? uint64_tinterval = (uint64_t)(1.0*NSEC_PER_SEC);
? ? dispatch_source_set_timer(self.timer, start, interval,0);
? ? // 設(shè)置回調(diào)
? ? dispatch_source_set_event_handler(self.timer, ^{
? ? ? ? NSLog(@"------------%@", [NSThread currentThread]);
? ? ? ? count++;
//? ? ? ? if (count == 4) {
//? ? ? ? ? ? // 取消定時(shí)器
//? ? ? ? ? ? dispatch_cancel(self.timer);
//? ? ? ? ? ? self.timer = nil;
//? ? ? ? }
? ? });