iOS常用定時器

來源:互聯網
上載者:User

標籤:

iOS常用定時器

UIScrollView 拖動時執行的是 UITrackingRunLoopMode,會導致暫停定時器,等恢複為 NSDefaultRunLoopMode 時才恢複定時器。所以如果需要定時器在 UIScrollView 拖動時也不影響的話,建議添加到 UITrackingRunLoopMode 或 NSRunLoopCommonModes 中:
NSTimer *timer = [NSTimer timerWithTimeInterval:5 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];[[NSRunLoop mainRunLoop] addTimer:timer forMode: UITrackingRunLoopMode]; ///< 或者 NSRunLoopCommonModes
  -------------------------------不怎麼華麗的分割線,以下為轉載內容------------------------------- 

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

然而,在iOS中有很多方法完成以上的任務,到底有多少種方法呢?經過查閱資料,大概有三種方法:NSTimer、CADisplayLink、GCD。接下來我就一一介紹它們的用法。

一、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:

1 NSTimer *timer = [NSTimer timerWithTimeInterval:5 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];2 [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
     二、CADisplayLinkCADisplayLink,需要加入QuartzCore.framework及#import。用CADisplayLink可以實現不停重繪簡要區別:NSTimer初始化器接受調用方法邏輯之間的間隔作為它的其中一個參數,預設一秒執行30次。CADisplayLink預設每秒運行60次,通過它的frameInterval屬性改變每秒運行幀數,如設定為2,意味CADisplayLink每隔一幀運行一次,有效邏輯每秒運行30次。此外,NSTimer接受另一個參數是否重複,而CADisplayLink預設為重複,直到它失效。還有一個區別在於,NSTimer一旦初始化它就開始運行,而CADisplayLink需要將顯示連結添加到一個運行迴圈中,即用於處理系統事件的一個Cocoa Touch結構。NSTimer 我們通常會用在背景計算,更新一些數值資料,而如果牽涉到畫面的更新,動畫過程的演變,我們通常會用CADisplayLink。

二、CADisplayLink

1. 建立方法

   self.displayLink= [CADisplayLinkdisplayLinkWithTarget:selfselector:@selector(handleDisplayLink:)];    [self.displayLinkaddToRunLoop:[NSRunLoopcurrentRunLoop]forMode:NSDefaultRunLoopMode];

2. 停止方法

    [self.displayLinkinvalidate];   self.displayLink=nil;4 /**當把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。

Demo地址:https://github.com/mrhyh/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.