美文网首页
YYKit-给NSTimer添加block

YYKit-给NSTimer添加block

作者: linbj | 来源:发表于2018-02-26 20:53 被阅读37次
    #import "NSTimer+YYAdd.h"
    #import "YYKitMacro.h"
    
    YYSYNTH_DUMMY_CLASS(NSTimer_YYAdd)
    
    @implementation NSTimer (YYAdd)
    
    //判断NSTimer是否有userinfo (传入的block),如果有就强转然后执行
    + (void)_yy_ExecBlock:(NSTimer *)timer {
        if ([timer userInfo]) {
            void (^block)(NSTimer *timer) = (void (^)(NSTimer *timer))[timer userInfo];
            block(timer);
        }
    }
    
    + (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)seconds block:(void (^)(NSTimer *timer))block repeats:(BOOL)repeats {
        return [NSTimer scheduledTimerWithTimeInterval:seconds target:self selector:@selector(_yy_ExecBlock:) userInfo:[block copy] repeats:repeats];
    }
    
    
    + (NSTimer *)timerWithTimeInterval:(NSTimeInterval)seconds block:(void (^)(NSTimer *timer))block repeats:(BOOL)repeats {
        
        return [NSTimer timerWithTimeInterval:seconds target:self selector:@selector(_yy_ExecBlock:) userInfo:[block copy] repeats:repeats];
    }
    
    @end
    

    相关文章

      网友评论

          本文标题:YYKit-给NSTimer添加block

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