iOS中幾種定時器

來源:互聯網
上載者:User

標籤:

在軟體開發過程中,我們常常需要在某個時間後執行某個方法,或者是按照某個周期一直執行某個方法。在這個時候,我們就需要用到定時器。

一、NSTimer

1. 建立方法

1 NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(action:) userInfo:nil repeats:NO];
  • TimerInterval : 執行之前等待的時間。比如設定成1.0,就代表1秒後執行方法

  • target : 需要執行方法的對象。

  • selector : 需要執行的方法

  • repeats : 是否需要迴圈

2. 釋放方法

1 [timer invalidate];
  • 注意 :

調用建立方法後,target對象的計數器會加1,直到執行完畢,自動減1。如果是迴圈執行的話,就必須手動關閉,否則可以不執行釋放方法。

3. 特性

  • 存在延遲

不 管是一次性的還是周期性的timer的實際觸發事件的時間,都會與所加入的RunLoop和RunLoop Mode有關,如果此RunLoop正在執行一個連續性的運算,timer就會被延時出發。重複性的timer遇到這種情況,如果延遲超過了一個周期,則 會在延時結束後立刻執行,並按照之前指定的周期繼續執行。

  • 必須加入Runloop

使用上面的建立方式,會自動把timer加入MainRunloop的NSDefaultRunLoopMode中。如果使用以下方式建立定時器,就必須手動加入Runloop:

12 NSTimer *timer = [NSTimer timerWithTimeInterval:5 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];

二、CADisplayLink

1. 建立方法

1234 ```objc    self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(handleDisplayLink:)];    [self.displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];```

2. 停止方法

12345 ```objc    [self.displayLink invalidate];  self.displayLink = nil;```          **當把CADisplayLink對象add到runloop中後,selector就能被周期性調用,類似於重複的NSTimer被啟動了;執行invalidate操作時,CADisplayLink對象就會從runloop中移除,selector調用也隨即停止,類似於NSTimer的invalidate方法。**

3. 特性

  • 螢幕重新整理時調用

    CADisplayLink 是一個能讓我們以和螢幕重新整理率同步的頻率將特定的內容畫到螢幕上的定時器類。CADisplayLink以特定模式註冊到runloop後,每當螢幕顯示 內容重新整理結束的時候,runloop就會向CADisplayLink指定的target發送一次指定的selector訊息, CADisplayLink類對應的selector就會被調用一次。所以通常情況下,按照iOS裝置螢幕的重新整理率60次/秒

  • 延遲

    • iOS裝置的螢幕重新整理頻率是固定的,CADisplayLink在正常情況下會在每次重新整理結束都被調用,精確度相當高。但如果調用的方法比較耗時,超過了螢幕重新整理周期,就會導致跳過若干次回調調用機會。

    • 如果CPU過於繁忙,無法保證螢幕60次/秒的重新整理率,就會導致跳過若干次調用回調方法的機會,跳過次數取決CPU的忙碌程度。

  • 使用情境

    從原理上可以看出,CADisplayLink適合做介面的不停重繪,比如視頻播放的時候需要不停地擷取下一幀用於介面渲染。

4. 重要屬性

  • frameInterval

    NSInteger類型的值,用來設定間隔多少幀調用一次selector方法,預設值是1,即每幀都調用一次。

  • duration

    readOnly 的CFTimeInterval值,表示兩次螢幕重新整理之間的時間間隔。需要注意的是,該屬性在target的selector被首次調用以後才會被賦值。 selector的調用間隔時間計算方式是:調用間隔時間 = duration × frameInterval。

三、GCD方式

  • 執行一次

12345 double delayInSeconds = 2.0;dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC); dispatch_after(popTime, dispatch_get_main_queue(), ^(void){     //執行事件});
  • 重複執行

12345678 NSTimeInterval period = 1.0; //設定時間間隔dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);dispatch_source_set_timer(_timer, dispatch_walltime(NULL, 0), period * NSEC_PER_SEC, 0); //每秒執行dispatch_source_set_event_handler(_timer, ^{    //在這裡執行事件});dispatch_resume(_timer);

iOS中幾種定時器

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.