Runloop定時(shí)器的應(yīng)用及關(guān)閉
iOS NSTimer 定時(shí)器用法總結(jié)
1.定時(shí)器的創(chuàng)建
- 參數(shù)1:時(shí)間間隔(定時(shí)的時(shí)間) 單位:秒
- 參數(shù)2:響應(yīng)消息的對(duì)象(調(diào)用方法的對(duì)象)
- 參數(shù)3:消息(方法),可以帶參數(shù),但是只能帶一個(gè)參數(shù),而且參數(shù)的實(shí)參就是當(dāng)前這個(gè)定時(shí)器對(duì)象本身
- 參數(shù)4:需要傳入到定時(shí)器中的對(duì)象,一般是nil
- 參數(shù)5:是否重復(fù)
功能:repeats是NO -> 間隔1秒的時(shí)間后,[self time]; repeats是YES -> 每隔1秒self去調(diào)用time一次
//定時(shí)器一創(chuàng)建就開(kāi)啟了
NSTimer scheduledTimerWithTimeInterval:<#(NSTimeInterval)#> target:<#(nonnull id)#> selector:<#(nonnull SEL)#> userInfo:<#(nullable id)#> repeats:<#(BOOL)#>
NSTimer * timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(time:) userInfo:@"123" repeats:YES];
NSLog(@"%p", timer);
2.暫停定時(shí)器
[timer setFireDate:[NSDate distantFuture]];
3.開(kāi)啟定時(shí)器 (默認(rèn):定時(shí)器一創(chuàng)建就自動(dòng)開(kāi)啟)
[timer setFireDate:[NSDate distantPast]];
4.讓定時(shí)器失效
[self.progressTimer invalidate];
//失效后再清空
self.progressTimer = nil;