CADisplayLink和其它CoreAnimation類同樣,都是在QuartzCore.framework裏。less
CADisplayLink最主要的特徵是能提供一個週期性的調用咱們賦給它的selector的機制,從這點上看它很像定時器NSTimer。oop
- (void)startDisplayLink { self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(handleDisplayLink:)]; [self.displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; } - (void)handleDisplayLink:(CADisplayLink *)displayLink { //do something } - (void)stopDisplayLink { [self.displayLink invalidate]; self.displayLink = nil; }
當把CADisplayLink對象add到runloop中後,selector就能被週期性調用,相似於NSTimer被啓動了;執行invalidate操做時, CADisplayLink對象就會從runloop中移除,selector 調用也隨即中止,相似於NSTimer的invalidate方法。atom
CADisplayLink是一個能讓咱們以和屏幕刷新率同步的頻率將特定的內容畫到屏幕上的定時器類。 CADisplayLink以特定模式註冊到runloop後, 每當屏幕顯示內容刷新結束的時候,runloop就會向 CADisplayLink指定的target發送一次指定的selector消息, CADisplayLink類對應的selector就會被調用一次。code
iOS設備的屏幕刷新頻率(FPS)是60Hz,所以CADisplayLink的selector 默認調用週期是每秒60次,這個週期能夠經過frameInterval屬性設置, CADisplayLink的selector每秒調用次數=60/ frameInterval。好比當 frameInterval設爲2,每秒調用就變成30次。orm
iOS設備的屏幕刷新頻率是固定的,CADisplayLink在正常狀況下會在每次刷新結束都被調用,精確度至關高。對象
CADisplayLink 使用場合相對專注, 適合作界面的不停重繪.ip
CADisplayLink 功能仍是比較單一的, 因此, 使用起來也是很簡單的, 暴露出來的方法和屬性只有以下幾個.ci
/** Class representing a timer bound to the display vsync. **/ @interface CADisplayLink : NSObject { @private void *_impl; } /* Create a new display link object for the main display. It will * invoke the method called 'sel' on 'target', the method has the * signature '(void)selector:(CADisplayLink *)sender'. */ + (CADisplayLink *)displayLinkWithTarget:(id)target selector:(SEL)sel; /* Adds the receiver to the given run-loop and mode. Unless paused, it * will fire every vsync until removed. Each object may only be added * to a single run-loop, but it may be added in multiple modes at once. * While added to a run-loop it will implicitly be retained. */ - (void)addToRunLoop:(NSRunLoop *)runloop forMode:(NSString *)mode; /* Removes the receiver from the given mode of the runloop. This will * implicitly release it when removed from the last mode it has been * registered for. */ - (void)removeFromRunLoop:(NSRunLoop *)runloop forMode:(NSString *)mode; /* Removes the object from all runloop modes (releasing the receiver if * it has been implicitly retained) and releases the 'target' object. */ - (void)invalidate; /* The current time, and duration of the display frame associated with * the most recent target invocation. Time is represented using the * normal Core Animation conventions, i.e. Mach host time converted to * seconds. */ @property(readonly, nonatomic) CFTimeInterval timestamp; @property(readonly, nonatomic) CFTimeInterval duration; /* When true the object is prevented from firing. Initial state is * false. */ @property(getter=isPaused, nonatomic) BOOL paused; /* Defines how many display frames must pass between each time the * display link fires. Default value is one, which means the display * link will fire for every display frame. Setting the interval to two * will cause the display link to fire every other display frame, and * so on. The behavior when using values less than one is undefined. */ @property(nonatomic) NSInteger frameInterval; @end