NSTimer 超时 block回调

最近做直播,由于经常用到定时器进行各种比如说心跳报文发送、进入后台2分钟自动关闭直播等操作,所以经常用到定时器的超时限制(即输入定时间隔和超时次数,定时器判断重复次数超过定时次数,清空定时次数,回调block,进入下一次循环),所以参考了这篇文章:

ios 给NSTimer添加Block回调版本创建方法

一、NSTimer+Extention.h

//  DeJiaIM
//  Created by fjf on 2016/12/6.
//  Copyright © 2016年 tsningning. All rights reserved.
//

#import 

typedef void(^NSTimerBlock)(void);

@interface NSTimer (Extention)

/**
 设置定时器

 @param interval 定时间隔
 @param repeats 是否重复
 @param callback 回调
 @return 返回定时器
 */
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)interval
                                repeats:(BOOL)repeats
                               timerCallback:(NSTimerBlock)callback;

/**
 设置定时器

 @param interval 定时间隔
 @param repeats 是否重复
 @param limitCount 超时次数
 @param timerCallback 回调
 @return 返回定时器
 */
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)interval
                                repeats:(BOOL)repeats
                             limitCount:(NSInteger)limitCount
                               timerCallback:(NSTimerBlock)timerCallback;
@end

二、NSTimer+Extention.m

//
//  NSTimer+Extention.m
//  DeJiaIM
//
//  Created by fjf on 2016/12/6.
//  Copyright © 2016年 tsningning. All rights reserved.
//

#import "NSTimer+Extention.h"

@implementation NSTimer (Extention)

+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)interval
                                repeats:(BOOL)repeats
                               timerCallback:(NSTimerBlock)timerCallback {

    return [NSTimer scheduledTimerWithTimeInterval:interval
                                        target:self
                                      selector:@selector(onTimerUpdateBlock:)
                                      userInfo:[timerCallback copy]
                                       repeats:repeats];
}

+ (void)onTimerUpdateBlock:(NSTimer *)timer {
    NSTimerBlock block = timer.userInfo;

    if (block) {
        block();
    }
}



+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)interval
                                  repeats:(BOOL)repeats
                                  limitCount:(NSInteger)limitCount
                               timerCallback:(NSTimerBlock)timerCallback {
    NSDictionary *userInfo = @{@"timerCallback"     : [timerCallback copy],
                           @"limitCount"        : @(limitCount)};

    return [NSTimer scheduledTimerWithTimeInterval:interval
                                        target:self
                                      selector:@selector(beyoudLimitTime:)
                                      userInfo:userInfo
                                       repeats:repeats];
}

+ (void)beyoudLimitTime:(NSTimer *)timer {
    static NSUInteger currentCount = 0;

    NSDictionary *userInfo = timer.userInfo;
    NSTimerBlock timerCallBack = userInfo[@"timerCallback"];
    NSNumber *count = userInfo[@"limitCount"];

    currentCount ++;

    if (currentCount > count.integerValue) {
        if (timerCallBack) {
            currentCount = 0;
            timerCallBack();
        }
    }
}

三.最后:

送上一张喜欢的图片:

NSTimer 超时 block回调_第1张图片
向往.jpg

如果觉得不错,麻烦给个喜欢或star,如果有问题请及时反馈,谢谢!

你可能感兴趣的:(NSTimer 超时 block回调)