美文网首页
NSTimer+Timer_Block

NSTimer+Timer_Block

作者: 白色天空729 | 来源:发表于2018-12-26 13:28 被阅读7次

NSTimer category

#import "NSTimer+Timer_Block.h"

typedef void(^timerBlock)(NSTimer *);

@implementation NSTimer (Timer_Block)

+ (NSTimer *)cq_scheduledTimerWithTimeInterval:(NSTimeInterval)interval repeats:(BOOL)repeats block:(timerBlock)block {
    return [self scheduledTimerWithTimeInterval:interval target:self selector:@selector(cq_callBlock:) userInfo:[block copy] repeats:repeats];
}

+ (NSTimer *)zdx_scheduledTimerWithTimeInterval:(NSTimeInterval)interval repeats:(BOOL)repeats block:(void(^)(NSTimer *))block {
    return [self scheduledTimerWithTimeInterval:interval target:self selector:@selector(cq_callBlock:) userInfo:[block copy] repeats:repeats];
}

+ (void)cq_callBlock:(NSTimer *)timer {
    void (^block)(NSTimer *timer) = timer.userInfo;
    !block ?: block(timer);
}
@end
#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface NSTimer (Timer_Block)

+ (NSTimer *)cq_scheduledTimerWithTimeInterval:(NSTimeInterval)interval repeats:(BOOL)repeats block:(void (^)(NSTimer *timer))block;

@end

NS_ASSUME_NONNULL_END

使用:

    __weak typeof(self) weakSelf = self;
    _timer = [NSTimer cq_scheduledTimerWithTimeInterval:1 repeats:YES block:^(NSTimer *timer) {
        __strong typeof(self) strongSelf = weakSelf;
        [strongSelf printNum];
    }];

相关文章

网友评论

      本文标题:NSTimer+Timer_Block

      本文链接:https://www.haomeiwen.com/subject/bvrhlqtx.html