NSTimer定時(shí)器類

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;
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容