NSTimer 簡(jiǎn)單使用方法

前言

NSTimer一般用來(lái)定時(shí),以便出發(fā)需要周期性執(zhí)行的任務(wù)或者間隔一定時(shí)間后執(zhí)行的任務(wù)。

使用方法

常用的啟動(dòng)定時(shí)器的方法有兩種:

+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti 
target:(id)aTarget 
selector:(SEL)aSelector 
userInfo:(nullable id)userInfo 
repeats:(BOOL)yesOrNo;

+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti 
target:(id)aTarget 
selector:(SEL)aSelector 
userInfo:(nullable id)userInfo 
repeats:(BOOL)yesOrNo;

停止方法

//通過(guò)invalidate方法可以停止定時(shí)器的工作,一旦定時(shí)器被停止了,就不能再次執(zhí)行任務(wù)。
//只能再創(chuàng)建一個(gè)新的定時(shí)器才能執(zhí)行新的任務(wù)
- (void)invalidate;

簡(jiǎn)便的方法(第一種)

+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti 
target:(id)aTarget 
selector:(SEL)aSelector 
userInfo:(nullable id)userInfo 
repeats:(BOOL)yesOrNo;
參數(shù):
(NSTimeInterval)ti  :延時(shí)時(shí)間(double類型)
target:(id)aTarget  :監(jiān)聽(tīng)時(shí)鐘觸發(fā)的對(duì)象
selector:(SEL)aSelector :時(shí)鐘觸發(fā)的方法
userInfo:(nullable id)userInfo :可以是任意對(duì)象,通常為nil
repeats:(BOOL)yesOrNo :是否重復(fù)觸發(fā)(為YES時(shí),每隔一定的延時(shí)時(shí)間就會(huì)觸發(fā)方法,為NO時(shí),從現(xiàn)在開(kāi)始間隔一定的延時(shí)時(shí)間就會(huì)觸發(fā),但只觸發(fā)一次)

是簡(jiǎn)便的方法,使用示例如下:

//每隔1秒就會(huì)觸發(fā)updateTime方法
self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateTime) userInfo:nil repeats:YES];

//定時(shí)器觸發(fā)方法
- (void)updateTime
{
   //定時(shí)時(shí)間到后會(huì)觸發(fā)改方法
}

這種方法使用時(shí)有弊端,就是當(dāng)界面有滾動(dòng)監(jiān)聽(tīng)時(shí)(如有一個(gè)UITextView),到了延時(shí)時(shí)間并不會(huì)去執(zhí)行觸發(fā)方法,等停止?jié)L動(dòng)時(shí)才會(huì)執(zhí)行觸發(fā)方法。
這就需要第二種啟動(dòng)定時(shí)器的方法。

第二種啟動(dòng)定時(shí)器的方法

+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti 
target:(id)aTarget 
selector:(SEL)aSelector 
userInfo:(nullable id)userInfo 
repeats:(BOOL)yesOrNo;

參數(shù):
(NSTimeInterval)ti  :延時(shí)時(shí)間(double類型)
target:(id)aTarget  :監(jiān)聽(tīng)時(shí)鐘觸發(fā)的對(duì)象
selector:(SEL)aSelector :時(shí)鐘觸發(fā)的方法
userInfo:(nullable id)userInfo :可以是任意對(duì)象,通常為nil
repeats:(BOOL)yesOrNo :是否重復(fù)觸發(fā)(為YES時(shí),每隔一定的延時(shí)時(shí)間就會(huì)觸發(fā)方法,為NO時(shí),從現(xiàn)在開(kāi)始間隔一定的延時(shí)時(shí)間就會(huì)觸發(fā),但只觸發(fā)一次)

使用示例:

self.timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(updateTime) userInfo:nil repeats:YES];
// 將timer添加到運(yùn)行循環(huán)
// 模式:NSRunLoopCommonModes的運(yùn)行循環(huán)模式(監(jiān)聽(tīng)滾動(dòng)模式)
[[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];

NSRunLoopCommonModes模式為監(jiān)聽(tīng)滾動(dòng)模式,將定時(shí)器加入該模式就和滾動(dòng)觸發(fā)事件在同一個(gè)等級(jí),這樣就不會(huì)受滾動(dòng)事件的影響了。

另外還有一種模式,DefaultRunLoopMode,默認(rèn)模式,加入這種模式就和第一種方法的效果相同了。

總結(jié)

所以說(shuō)使用NSTimer來(lái)定時(shí)其定時(shí)有時(shí)候會(huì)不準(zhǔn)確,通常使用NSTimer來(lái)觸發(fā)時(shí)間間隔相對(duì)較長(zhǎng),且如果不準(zhǔn)確對(duì)后果無(wú)太大影響的事件。
如果要出發(fā)時(shí)間間隔較短的事件可以使用CADisplayLink CADisplayLink參考

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容