iOS开发-Quartz2D绘制时定时器选择

NSTimer定时器
[NSTimer scheduledTimerWithTimeInterval:0.025 target:self selector:@selector(update) userInfo:nil repeats:YES];
//如果我们使用NSTimer定时器. 设置的执⾏行时间为0.025秒, 假如屏幕刷新时间为0.035.中间就会等待0.010
//绘制图形的时候不建议使用该方法
CADisplayLink定时器
补充知识:
drawRect方法是在view将要显示,已经显示之间调用的
drawRect方法是不能手动调⽤,因为在drawRect方法中才能获取跟View相关联的上下文, 系统在调用DrawRect方法时,会⾃动帮你创建一个跟View相关联的上下文,并且传递给它
解决办法:想要重绘,调⽤[self setNeedsDisplay]; 告诉系统重新绘制View,系统就会自动帮你调用drawRect方法,系统在调用 drawRect方法,它会帮你创建上下文
//setNeedsDisplay:会调用drawRect,但是它并不是立马调用的,只是设置一个标志.当下一次屏幕刷新的时候.
//我们在绘制的时候使用定时器最好使用CADisplayLink. 创建CADisplayLink定时器 这个定时器⽅法它是当每次屏幕刷新的时候调⽤(屏幕每一秒刷新60次);
//使⽤用CADisplayLink不需要考虑时间间隔.
CADisplayLink *link = [CADisplayLink displayLinkWithTarget:self selector:@selector(update)];
//要让它工作, 必须得要把定时器添加到主运行循环
[link addToRunLoop:[NSRunLoop mainRunLoop]forMode:NSDefaultRunLoopMode];
原文地址:https://www.cnblogs.com/liugengqun/p/5169082.html