NSTimer封装

2022-12-11  本文已影响0人  和女神经常玩

接口部分

@interface NSTimer (SGExtend)

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

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

@end

实现部分

@implementation NSTimer (Extend)

+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)interval repeats:(BOOL)repeats block:(void (^)(NSTimer *timer))block
{
    NSTimer* timer = [NSTimer timerWithTimeInterval:interval target:self selector:@selector(timered:) userInfo:block repeats:repeats];
    return timer;
}

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

+ (void)timered:(NSTimer *)timer
{
    void (^block)(NSTimer *timer) = timer.userInfo;
    block(timer);
}
@end
上一篇 下一篇

猜你喜欢

热点阅读