[转]NSTimer和CADisplayLink的基本用法
2015-10-27 16:47
246 查看
简要区别:
NSTimer初始化器接受调用方法逻辑之间的间隔作为它的其中一个参数,预设一秒执行30次。
CADisplayLink默认每秒运行60次,通过它的frameInterval属性改变每秒运行帧数,如设置为2,意味CADisplayLink每隔一帧运行一次,有效的逻辑每秒运行30次。
此外,NSTimer接受另一个参数是否重复,而CADisplayLink默认为重复,直到它失效。
还有一个区别在于,NSTimer一旦初始化它就开始运行,而CADisplayLink需要将显示链接添加到一个运行循环中,即用于处理系统事件的一个Cocoa Touch结构。
NSTimer 我们通常会用在背景计算,更新一些数值资料,而如果牵涉到画面的更新,动画过程的演变,我们通常会用CADisplayLink。
NSTimer
CADisplayLink,需要加入QuartzCore.framework及#import
/*CADisplayLink 默认每秒运行60次,将它的frameInterval属性设置为2,意味CADisplayLink每隔一帧运行一次,有效的使游戏逻辑每秒运行30次*/
NSTimer初始化器接受调用方法逻辑之间的间隔作为它的其中一个参数,预设一秒执行30次。
CADisplayLink默认每秒运行60次,通过它的frameInterval属性改变每秒运行帧数,如设置为2,意味CADisplayLink每隔一帧运行一次,有效的逻辑每秒运行30次。
此外,NSTimer接受另一个参数是否重复,而CADisplayLink默认为重复,直到它失效。
还有一个区别在于,NSTimer一旦初始化它就开始运行,而CADisplayLink需要将显示链接添加到一个运行循环中,即用于处理系统事件的一个Cocoa Touch结构。
NSTimer 我们通常会用在背景计算,更新一些数值资料,而如果牵涉到画面的更新,动画过程的演变,我们通常会用CADisplayLink。
NSTimer
@interface ViewController : UIViewController { NSTimer *theTimer; //声明 } //使用 float theInterval = 1.0 / 30.0f; //每秒调用30次 theTimer = [NSTimer scheduledTimerWithTimeInterval:theInterval target:self selector:@selector(MyTask) userInfo:nil repeats:YES]; //停用 [theTimer invalidate]; theTimer = nil;
CADisplayLink,需要加入QuartzCore.framework及#import
/*CADisplayLink 默认每秒运行60次,将它的frameInterval属性设置为2,意味CADisplayLink每隔一帧运行一次,有效的使游戏逻辑每秒运行30次*/
f(theTimer == nil) { theTimer = [CADisplayLink displayLinkWithTarget:self selector:@selector(MyTask)]; theTimer.frameInterval = 2; [theTimer addToRunLoop: [NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; } //停用 [theTimer invalidate]; theTimer = nil;
相关文章推荐
- Hdu 4778 Gems Fight! (状态压缩 + DP)
- 021.forUndefinedKey 处理不存在的key
- Unity3D加载4种机制
- 使用ngrok让微信公众平台通过80端口访问本机
- Java基础学习9(一维数组及在内存中的分配)
- cuda纹理内存简单操作 (2015年)
- 3.1
- zabbix监控端口状态
- 护卫神,服务器安全专家!
- Myeclipse中怎么将所有的jsp文件编码格式调整为GBK
- 利用runtime实现自定义对象归档总结
- 排序与list.sort()使用
- java中的[Ljava.lang.Object;@2a139a55问题
- JSP的学习(4)——中文乱码的解决
- Servlet3.0中Servlet的使用
- 随记
- 奇妙的NullPointerException
- java string
- 023.Key 路径
- nonatomic, retain,weak,strong用法详解