ios 给NSTimer添加Block方法

NSTimer本没有现成API支持添加Block方法 但是有些变态面试题废除这种恶心的东西,so 搞了一下当练手啦

#import   
  
@interface NSTimer (BlockMethod)  
  
/** 
 *  无参数无返回值Block 
 */  
typedef void (^HYBVoidBlock)(void);  
  
/** 
 *  创建Timer---Block版本 
 * 
 *  @param interval 每隔interval秒就回调一次callback 
 *  @param repeats  是否重复 
 *  @param callback 回调block 
 * 
 *  @return NSTimer对象 
 */  
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)interval  
                                    repeats:(BOOL)repeats  
                                   callback:(HYBVoidBlock)callback;  
  
/** 
 *  创建Timer---Block版本 
 * 
 *  @param interval 每隔interval秒就回调一次callback 
 *  @param count  回调多少次后自动暂停,如果count <= 0,则表示无限次,否则表示具体的次数 
 *  @param callback 回调block 
 * 
 *  @return NSTimer对象 
 */  
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)interval  
                                    count:(NSInteger)count  
                                   callback:(HYBVoidBlock)callback;  
  
/** 
 *  开始启动定时器 
 */  
- (void)fireTimer;  
  
/** 
 *  暂停定时器 
 */  
- (void)unfireTimer;  
  
  
@end  
//
//  NSTimer+LGBlockMethod.m
//  nsoperationQueueDemo
//
//  Created by 张传奇 on 2018/3/20.
//  Copyright © 2018年 张传奇. All rights reserved.
//

#import "NSTimer+LGBlockMethod.h"

@implementation NSTimer (LGBlockMethod)
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)interval
                                    repeats:(BOOL)repeats
                                   callback:(LGVoidBlock)callback {
    return [NSTimer scheduledTimerWithTimeInterval:interval
                                            target:self
                                          selector:@selector(onTimerUpdateBlock:)
                                          userInfo:[callback copy]
                                           repeats:repeats];
}

+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)interval
                                      count:(NSInteger)count
                                   callback:(LGVoidBlock)callback {
    NSDictionary *userInfo = @{@"callback"     : [callback copy],
                               @"count"        : @(count)};
    return [NSTimer scheduledTimerWithTimeInterval:interval
                                            target:self
                                          selector:@selector(onTimerUpdateCountBlock:)
                                          userInfo:userInfo
                                           repeats:YES];
}

+ (void)onTimerUpdateBlock:(NSTimer *)timer {
    LGVoidBlock block = timer.userInfo;
    
    if (block) {
        block();
    }
}

+ (void)onTimerUpdateCountBlock:(NSTimer *)timer {
    static NSUInteger currentCount = 0;
    
    NSDictionary *userInfo = timer.userInfo;
    LGVoidBlock callback = userInfo[@"callback"];
    NSNumber *count = userInfo[@"count"];
    
    if (count.integerValue <= 0) {
        if (callback) {
            callback();
        }
    } else {
        if (currentCount < count.integerValue) {
            currentCount++;
            if (callback) {
                callback();
            }
        } else {
            currentCount = 0;
            
            [timer stopTimer];
        }
    }
}

- (void)startTimer {
    [self setFireDate:[NSDate distantPast]];
}

- (void)stopTimer {
    [self setFireDate:[NSDate distantFuture]];
}

@end

你可能感兴趣的:(ios 给NSTimer添加Block方法)