什么是CADisplayLink
點進(jìn)CADisplayLink的頭文件我們能看到,其實他的方法并不多,而且他的功能很單一,就是作為一個定時器的存在。
不過既然蘋果專門提供了這么一個類,就一定是有他的存在意義的。他的優(yōu)勢就在于他的執(zhí)行頻率是根據(jù)設(shè)備屏幕的刷新頻率來計算的。換句話講,他也是時間間隔最準(zhǔn)確的定時器。
使用實例
定義屬性
@interface ViewController ()
@property(strong,nonatomic) CADisplayLink *link;
@property(strong,nonatomic) UIImageView *imageView;
@end
- (void)viewDidLoad {
[super viewDidLoad];
self.imageView=[[UIImageView alloc] initWithFrame:CGRectMake(160, 160, 50, 50)];
self.imageView.image=[UIImage imageNamed:@"loading_blu"];
[self.view addSubview:self.imageView];
[self.link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
}
- (CADisplayLink *)link
{
if (!_link) {
self.link = [CADisplayLink displayLinkWithTarget:self selector:@selector(up)];
}
return _link;
}
- (void)up
{
// 規(guī)定時間內(nèi)轉(zhuǎn)動的角度 == 時間 * 速度
CGFloat ang = self.link.duration * 5;
self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, ang);
}

效果圖.gif