2018-04-08

2018-04-08  本文已影响5人  被风吹乱的思念

GCD定时循环

// GCD定时器
    static dispatch_source_t _timer;
    NSTimeInterval period = 1.0; //设置时间间隔
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    dispatch_source_set_timer(_timer, dispatch_walltime(NULL, 0), period * NSEC_PER_SEC, 0); //每秒执行
    // 事件回调
    dispatch_source_set_event_handler(_timer, ^{
        dispatch_async(dispatch_get_main_queue(), ^{
            NSLog(@"Count");
        });
    });
        
    // 开启定时器
    dispatch_resume(_timer);
        
    // 关闭定时器
    // dispatch_source_cancel(_timer);
上一篇 下一篇

猜你喜欢

热点阅读