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