官方提供的接口如下:
#import <QuartzCore/CABase.h>
#import <Foundation/NSObject.h>
@class NSString, NSRunLoop;
NS_ASSUME_NONNULL_BEGIN
/** Class representing a timer bound to the display vsync. **/
CA_CLASS_AVAILABLE_IOS(3.1, 9.0, 2.0)
@interface CADisplayLink : NSObject
{
@private
void *_impl;
}
/*创建CADisplayLink对象*/
+ (CADisplayLink *)displayLinkWithTarget:(id)target selector:(SEL)sel;
/*将CADisplayLink对象添加到RunLoop中*/
- (void)addToRunLoop:(NSRunLoop *)runloop forMode:(NSRunLoopMode)mode;
/*将CADisplayLink对象移除从RunLoop中*/
- (void)removeFromRunLoop:(NSRunLoop *)runloop forMode:(NSRunLoopMode)mode;
/*销毁CADisplayLink对象*/
- (void)invalidate;
/*表示图像上一帧显示的时间*/
@property(readonly, nonatomic) CFTimeInterval timestamp;
/*表示两次屏幕刷新之间的时间间隔*/
@property(readonly, nonatomic) CFTimeInterval duration;
/*目标对象方法被调用的下一次时间点*/
@property(readonly, nonatomic) CFTimeInterval targetTimestamp;
/*是否暂停向目标对象调用的方法:值为YES来停止目标对象方法的执行。*/
@property(getter=isPaused, nonatomic) BOOL paused;
/*帧间隔*/
@property(nonatomic) NSInteger frameInterval;
/*每一秒显示多少帧*/
@property(nonatomic) NSInteger preferredFramesPerSecond;
@end
NS_ASSUME_NONNULL_END
使用方法:
- (void)startDisplayLink {
self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(displayLinkAction:)];
[self.displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
}
- (void)displayLinkAction:(CADisplayLink *)displayLink {
//do something
}
- (void)invalidateDisplayLink {
[self.displayLink invalidate];
self.displayLink = nil;
}
- (void)removeDisplayLink {
[self.displayLink removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
}