NSTimer和CADisplayLink的基本用法
来源:互联网 发布:淘宝联盟引流教程 编辑:程序博客网 时间:2024/05/21 23:17
简要区别:
NSTimer初始化器接受调用方法逻辑之间的间隔作为它的其中一个参数,预设一秒执行30次。
CADisplayLink默认每秒运行60次,通过它的frameInterval属性改变每秒运行帧数,如设置为2,意味CADisplayLink每隔一帧运行一次,有效的逻辑每秒运行30次。
此外,NSTimer接受另一个参数是否重复,而CADisplayLink默认为重复,直到它失效。
还有一个区别在于,NSTimer一旦初始化它就开始运行,而CADisplayLink需要将显示链接添加到一个运行循环中,即用于处理系统事件的一个Cocoa Touch结构。
NSTimer 我们通常会用在背景计算,更新一些数值资料,而如果牵涉到画面的更新,动画过程的演变,我们通常会用CADisplayLink。
NSTimer
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
@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次/
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
if(theTimer == nil){ theTimer = [CADisplayLink displayLinkWithTarget:self selector:@selector(MyTask)]; theTimer.frameInterval = 2; [theTimer addToRunLoop: [NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];}
//停用
[theTimer invalidate];
theTimer = nil;
文章链接:http://www.flakor.cn/2015-03-28-876.html
- NSTimer和CADisplayLink的基本用法
- NSTimer和CADisplayLink的基本用法
- NSTimer和CADisplayLink的基本用法
- NSTimer和CADisplayLink的基本用法
- NSTimer和CADisplayLink的基本用法
- NSTimer和CADisplayLink的用法
- NSTimer和CADisplayLink的用法
- IOS--NSTimer和CADisplayLink的用法
- IOS 中 NSTimer和CADisplayLink的用法
- CADisplayLink和NSTimer的区别
- CADisplayLink 和 NSTimer 的差异
- CADisplayLink和NSTimer的区别
- iOS - 计时器NSTimer 和CADisplayLink用法
- NSTimer、CADisplayLink 和 GCD 定时器用法
- CADisplayLink以及和NSTimer的区别
- CADisplayLink以及和NSTimer的区别
- 定时器的使用NStimer 和CADisplayLink
- iOS CADisplayLink以及和NSTimer的区别
- eclipse mat插件本地安装
- 深度优先搜索——红与黑
- android Sqlite SQL语句
- python变量
- APache环境变量设置
- NSTimer和CADisplayLink的基本用法
- 如何实现webfont自定义字体方案
- cocos2d从零开始
- 修改linux ulimit 限制
- 用Eclipse的snippets功能实现代码重用
- 黑马程序员——Java基础---面向对象(继承_概述、关键字final、抽象类)(5) 分类: Java学习笔记
- vim集成开发环境搭建之ctags配置使用
- python字典
- 匈牙利算法改进 之 队列优化寻找极大增广路径集 —— Hopcroft-Karp算法 【记录】