日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

iOS中定时器NSTimer的使用

發布時間:2024/4/14 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 iOS中定时器NSTimer的使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1、初始化

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

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

注:不用scheduled方式初始化的,需要手動addTimer:forMode: 將timer添加到一個runloop中。

  而scheduled的初始化方法將以默認mode直接添加到當前的runloop中.


舉例:

NSTimer *timer?= [NSTimer?scheduledTimerWithTimeInterval:10.0?target:self?selector:@selector(timerFired:)?userInfo:nil?repeats:NO];

NSTimer *myTimer = [NSTimertimerWithTimeInterval:3.0?target:selfselector:@selector(timerFired:) userInfo:nilrepeats:NO];

[[NSRunLoopcurrentRunLoop] addTimer:myTimerforMode:NSDefaultRunLoopMode];

?

2、觸發(啟動)

當定時器創建完(不用scheduled的,添加到runloop中后,該定時器將在初始化時指定的timeInterval秒后自動觸發。


可以使用-(void)fire;方法來立即觸發該定時器;

注:You can use this method to fire a repeating timer without interrupting its regular firing schedule. If the timer is non-repeating, it is automatically invalidated after firing, even if its scheduled fire date has not arrived.

在重復執行的定時器中調用此方法后立即觸發該定時器,但不會中斷其之前的執行計劃;

在不重復執行的定時器中調用此方法,立即觸發后,就會使這個定時器失效。

?

3、停止

- (void)invalidate;

這個是唯一一個可以將計時器從runloop中移出的方法。

?

注:

NSTimer可以精確到50-100毫秒.

NSTimer不是絕對準確的,而且中間耗時或阻塞錯過下一個點,那么下一個點就pass過去了.


轉載于:https://www.cnblogs.com/zhulin/archive/2012/02/02/2335866.html

總結

以上是生活随笔為你收集整理的iOS中定时器NSTimer的使用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。