/** 定時器(這里不用帶*,因為dispatch_source_t就是個類,內部已經包含了*) */
@property?(nonatomic,?strong)?dispatch_source_t?timer;
int?count?=?0;
-?(void)touchesBegan:(NSSet?*)touches?withEvent:(UIEvent?*)event
{
//?獲得隊列
//????dispatch_queue_t?queue?=?dispatch_get_global_queue(0,?0);
dispatch_queue_t?queue?=?dispatch_get_main_queue();
//?創建一個定時器(dispatch_source_t本質還是個OC對象)
self.timer?=?dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER,?0,?0,?queue);
//?設置定時器的各種屬性(幾時開始任務,每隔多長時間執行一次)
//?GCD的時間參數,一般是納秒?NSEC_PER_SEC(1秒?==?10的9次方納秒)
//?何時開始執行第一個任務
//?dispatch_time(DISPATCH_TIME_NOW,?3.0?*?NSEC_PER_SEC)?比當前時間晚3秒
dispatch_time_t?start?=?dispatch_time(DISPATCH_TIME_NOW,?(int64_t)(1.0?*?NSEC_PER_SEC));
uint64_t?interval?=?(uint64_t)(1.0?*?NSEC_PER_SEC);
dispatch_source_set_timer(self.timer,?start,?interval,?0);
//?設置回調
dispatch_source_set_event_handler(self.timer,?^{
NSLog(@"------------%@",?[NSThread?currentThread]);
count++;
//????????if?(count?==?4)?{
//????????????//?取消定時器
//????????????dispatch_cancel(self.timer);
//????????????self.timer?=?nil;
//????????}
});
//?啟動定時器
dispatch_resume(self.timer);