美文网首页
NSTimer封装

NSTimer封装

作者: 和女神经常玩 | 来源:发表于2022-12-11 23:26 被阅读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

相关文章

网友评论

      本文标题:NSTimer封装

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