iOS GCD定时器

GCD定时器的优势是不受RunLoop的运行模式的影响,它的的间隔时间是纳秒为单位。

GCDTimer.h

#import <Foundation/Foundation.h>

typedef void (^TimerRunBlock)(NSUInteger currentTime);  //定时器计时回调
typedef void (^TimerStopBlock)();                       //定时器结束计时回调

@interface DPGCDTimer : NSObject

@property (nonatomic,assign)BOOL isTimerRuning;         //定时器是否正在运行
@property (nonatomic, copy) TimerStopBlock timerStopBlock;

/**
 *  定时器初始化(定时器默认开启)
 *
 *  @param timeDuration  时长
 *  @param timerRunBlock 定时器回调
 */
- (void)timerWithTimeDuration:(double)timeDuration withRunBlock:(TimerRunBlock)timerRunBlock;


/**
 *  定时器停止
 */
- (void)stopTimer;

/**
 *  定时器恢复
 */
- (void)resumeTimer;

/**
 *  定时器暂停
 */
- (void)suspendTimer;

@end
#import "DPGCDTimer.h"
#define WS(weakSelf)  __weak __typeof(&*self)weakSelf = self;

@interface DPGCDTimer ()
{
    dispatch_source_t __block timer;
}

@property (nonatomic,copy)TimerRunBlock timerRunBlock;

@end

@implementation DPGCDTimer

- (void)timerWithTimeDuration:(double)timeDuration withRunBlock:(TimerRunBlock)timerRunBlock
{
    if (!timer) {
        WS(weakSelf);
        //创建定时器
        _isTimerRuning = YES;
        NSUInteger __block time = timeDuration;
        
        self.timerRunBlock = timerRunBlock;
        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),1 * NSEC_PER_SEC, 0); //每秒执行
        dispatch_source_set_event_handler(timer, ^{
            if (time <= 0) {
                [self stopTimer];
            } else {
                if (weakSelf.timerRunBlock) {
                    weakSelf.timerRunBlock(time --);
                }
            }
        });
        dispatch_resume(timer);
    }
    
}

//停止
- (void)stopTimer
{
    if(timer){
        if (_isTimerRuning) {
            dispatch_source_cancel(timer);
            timer = nil;
            _isTimerRuning = NO;
            if (self.timerStopBlock) {
                self.timerStopBlock();
            }
        }
        
    }
}

//恢复
- (void)resumeTimer
{
    if(timer){
        if (!_isTimerRuning) {
            dispatch_resume(timer);
            _isTimerRuning = YES;
        }
        
    }
}

//暂停
- (void)suspendTimer
{
    if(timer){
        if (_isTimerRuning) {
            dispatch_suspend(timer);
            _isTimerRuning = NO;
        }
    }
}


@end

PS:
GCD 中刷新UI导致线程崩溃问题工具 - PSPDFUIKitMainThreadGuard
工具使用方法就是把这个.m文件拖入工程,如果有问题就会PSPDFAssert断点处停住。在左侧线程分析中点击查看出现错误的地方。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 最近看了一下 iOS 的定时器的使用,我们来模拟一个发送短信验证码时候的等待计时动画 要想了解定时器,首先需要了解...
    TomatosX阅读 1,158评论 0 0
  • 前言 最近离职了,可以尽情熬夜写点总结,不用担心第二天上班爽并蛋疼着,这篇的主角 RunLoop 一座大山,涵盖的...
    zerocc2014阅读 12,434评论 13 67
  • 本文首发于我的个人博客:「程序员充电站」[https://itcharge.cn]文章链接:「传送门」[https...
    ITCharge阅读 60,789评论 50 540
  • 1,NSObject中description属性的意义,它可以重写吗?答案:每当 NSLog(@"")函数中出现 ...
    eightzg阅读 4,192评论 2 19
  • *面试心声:其实这些题本人都没怎么背,但是在上海 两周半 面了大约10家 收到差不多3个offer,总结起来就是把...
    Dove_iOS阅读 27,211评论 30 472