GCD定时器

来源:互联网 发布:nba骑士数据 编辑:程序博客网 时间:2024/05/22 18:22
// 比NSTimer更精准,不受RunLoop的Model影响int count = 0;- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{    // 获得队列    dispatch_queue_t queue = dispatch_get_global_queue(0, 0);    // 1 创建一个定时器    self.timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);//    dispatch_time_t start = dispatch_time(DISPATCH_TIME_NOW, 3.0 * NSEC_PER_SEC);//    dispatch_time_t interval = 2.0 * NSEC_PER_SEC;    dispatch_source_set_timer(self.timer, DISPATCH_TIME_NOW, 2.0 * NSEC_PER_SEC, 0.0 * NSEC_PER_SEC);    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);}

0 0
原创粉丝点击