iOS

CADisplayLink 详解

2019-06-10  本文已影响0人  木易林1
API_AVAILABLE(macos(10.14), ios(3.1), watchos(2.0), tvos(9.0)) //适用的系统版本
@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:(NSRunLoopMode)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:(NSRunLoopMode)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;

/* The next timestamp that the client should target their render for. */

@property(readonly, nonatomic) CFTimeInterval targetTimestamp
    API_AVAILABLE(macos(10.14), ios(10.0), watchos(3.0), tvos(10.0));

/* 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.
 * DEPRECATED - use preferredFramesPerSecond. */

@property(nonatomic) NSInteger frameInterval
  API_DEPRECATED("preferredFramesPerSecond", ios(3.1, 10.0), 
                 watchos(2.0, 3.0), tvos(9.0, 10.0));

/* Defines the desired callback rate in frames-per-second for this display
 * link. If set to zero, the default value, the display link will fire at the
 * native cadence of the display hardware. The display link will make a
 * best-effort attempt at issuing callbacks at the requested rate. */

@property(nonatomic) NSInteger preferredFramesPerSecond
    API_AVAILABLE(ios(10.0), watchos(3.0), tvos(10.0));

CADisplayLink 是一种计时器

常用的计时器有CADisplayLinkNSTimer

CADisplayLink概括

CADisplayLink`是用于同步屏幕刷新频率的计时器

初始化

通过+ (CADisplayLink *)displayLinkWithTarget:(id)target selector:(SEL)sel初始化link对象

添加到runloop

如果想开启link需要把link加入到runloop中: - (void)addToRunLoop:(NSRunLoop *)runloop forMode:(NSRunLoopMode)mode.除非计时器被停止,否则每次屏幕刷新时,计时器的方法都会被触发.

从runloop中移除

移除计时器有两个方法:- (void)removeFromRunLoop:(NSRunLoop *)runloop forMode:(NSRunLoopMode)mode- (void)invalidate.

时间戳

时间戳timestamp,这个属性用来返回上一次屏幕刷新的时间戳.如果视频播放的应用,可以通过时间戳来获取上一帧的具体时间,来计算下一帧.

间隔时间

duration属性用于提供屏幕最大刷新频率(maximumFramesPerSecond)下每一帧的时间间隔.这个属性可以用于在应用中获取帧率.

修改帧率 : 如果在特定帧率内无法提供对象的操作,可以通过降低帧率解决.一个拥有持续稳定但是较慢帧率的应用要比跳帧的应用顺滑的多.
可以通过preferredFramesPerSecond来设置每秒刷新次数.preferredFramesPerSecond默认值为屏幕最大帧率(maximumFramesPerSecond),目前是60.
实际的屏幕帧率会和preferredFramesPerSecond有一定的出入,结果是由设置的值和屏幕最大帧率(maximumFramesPerSecond)相互影响产生的.

如果屏幕最大帧率(preferredFramesPerSecond)是60,实际帧率只能是15, 20, 30, 60中的一种.如果设置大于60的值,屏幕实际帧率为60.如果设置的是26~35之间的值,实际帧率是30.如果设置为0,会使用最高帧率.

PS:

CADisplayLink是不能被继承的.

上一篇下一篇

猜你喜欢

热点阅读