IOS NSTimer
有三种简单的使用方式:
第一种:
//NSTimeInterval 间隔时间
//<#(nonnull id)#> 实施目标
//<#(nonnull SEL)#> 需要执行的方法
//repeats:<#(BOOL)#>] 是否重复
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:<#(NSTimeInterval)#> target:<#(nonnull id)#> selector:<#(nonnull SEL)#> userInfo:<#(nullable id)#> repeats:<#(BOOL)#>];
[timer invalidate];//将其设置为无效
第二种:
//初始化一个Invocation对象NSInvocation * invo = [NSInvocation invocationWithMethodSignature:[[selfclass] instanceMethodSignatureForSelector:@selector(init)]];
[invo setTarget:self];
[invo setSelector:@selector(myLog)];
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:<#(NSTimeInterval)#> invocation:invo repeats:<#(BOOL)#>];
[timer invalidate];//将其设置为无效
第三种:
//<#(NSTimeInterval)#> 间隔时间
//<#^(NSTimer *timer)inBlock#> 执行代码块
//是否重复
[NSTimer bk_scheduledTimerWithTimeInterval:<#(NSTimeInterval)#> block:<#^(NSTimer *timer)inBlock#> repeats:<#(BOOL)#>];
注意:NSTimer 中fire 和invalidate的区别;
invalidate
Stops the receiver from ever firing again and requests its removal from its run loop
This method is the only way to remove a timer from an NSRunLoop object
fire
Causes the receiver’s message to be sent to its target
If the timer is non-repeating, it is automatically invalidated after firing