第一种:
用CADisplayLink可以实现不停重绘。
例子:
 

CADisplayLink* gameTimer;

gameTimer = [CADisplayLink displayLinkWithTarget:self

selector:@selector(updateDisplay:)];

[gameTimer addToRunLoop:[NSRunLoop currentRunLoop]

forMode:NSDefaultRunLoopMode];


第二种:
int CCApplication::run()
{
if (applicationDidFinishLaunching())
{
[[CCDirectorCaller sharedDirectorCaller] startMainLoop];//主循环开始
}
return 0;
}
继续跟进startMainLoop函数
-(void) startMainLoop
{
// CCDirector::setAnimationInterval() is called, we should invalidate it first
[displayLink invalidate];
displayLink = nil;
// displayLink是CADisplayLink对象,target是自己,回调是coCaller
displayLink = [NSClassFromString(@"CADisplayLink") displayLinkWithTarget:self selector:@selector(doCaller:)];//看这个doCaller回调
[displayLink setFrameInterval: self.interval];//设置帧率
[displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];//添加到循环并启动
}
看doCaller回调,
void CCDisplayLinkDirector::mainLoop(void)
{
if (m_bPurgeDirecotorInNextLoop)
{
m_bPurgeDirecotorInNextLoop = false;
purgeDirector();
}
else if (! m_bInvalid)
{
drawScene();// draw the scene // release the objects
CCPoolManager::sharedPoolManager()->pop();
}
}

好,一个循环完了。最后看到CCPoolManager::sharedPoolManager()->pop();就是用来释放对象的。

 
 
第三种:

IOS--NSTimer和CADisplayLink的用法

NSTimer初始化器接受调用方法逻辑之间的间隔作为它的其中一个参数,预设一秒执行30次。CADisplayLink默认每秒运行60次,通过它的frameInterval属性改变每秒运行帧数,如设置为2,意味CADisplayLink每隔一帧运行一次,有效的逻辑每秒运行30次。

此外,NSTimer接受另一个参数是否重复,而把CADisplayLink设置为重复(默认重复?)直到它失效。

还有一个区别在于,NSTimer一旦初始化它就开始运行,而CADisplayLink需要将显示链接添加到一个运行循环中,即用于处理系统事件的一个Cocoa Touch结构。

NSTimer 我们通常会用在背景计算,更新一些数值资料,而如果牵涉到画面的更新,动画过程的演变,我们通常会用CADisplayLink。

但是要使用CADisplayLink,需要加入QuartzCore.framework及#import <QuartzCore/CADisplayLink.h>

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 <QuartzCore/CADisplayLink.h>

/*CADisplayLink 默认每秒运行60次,将它的frameInterval属性设置为2,意味CADisplayLink每隔一帧运行一次,有效的使游戏逻辑每秒运行30次*/

if(theTimer == nil)

{

theTimer = [CADisplayLink displayLinkWithTarget:self selector:@selector(MyTask)];

theTimer.frameInterval = 2;

[theTimer addToRunLoop: [NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

}

//停用

[theTimer invalidate];

theTimer = nil;

CADisplayLink 及定时器的使用的更多相关文章

  1. CADisplayLink以及定时器的使用

    第一种: 用CADisplayLink可以实现不停重绘. - (CADisplayLink *)link { if (!_link) { // 创建定时器,一秒钟调用rotation方法60次 _li ...

  2. iOS 音乐

    iOS 音乐 这篇博客介绍的是较长的音频播放-音乐... 有关音效的介绍可以点击下面的连接进入... iOS 音效 首先导入框架:AVFoundation.framework 导入框架后我们在需要使用 ...

  3. iOS 水波效果

    将水波效果放在子视图上,主控制器只负责加载 #import "WaveProgress.h" @interface WaveProgress () @property (nonat ...

  4. iOS开发Quartz2D之 七:雪花效果

    #import "VCView.h" @implementation VCView -(void)awakeFromNib { //[NSTimer scheduledTimerW ...

  5. Objective-C三种定时器CADisplayLink / NSTimer / GCD的使用

    OC中的三种定时器:CADisplayLink.NSTimer.GCD 我们先来看看CADiskplayLink, 点进头文件里面看看, 用注释来说明下 @interface CADisplayLin ...

  6. iOS CADisplayLink 定时器的使用

    CADisplayLink 是一个能让我们以和屏幕刷新频率相同的频率将内容刻画到屏幕上的定时器,在应用中创建一个新的CADisplayLink对象,把他添加到一个runloop中,并且给他提供一个ta ...

  7. 运用CADisplayLink来开启定时器

    CADisplayLink来开启定时器 CADisplayLink是以屏幕刷新频率将内容绘制到屏幕上的定时器,每秒60Hz.使用的时候,先创建一个CADisplayLink对象,将其添加到一个RunL ...

  8. 定时器 NSTimer 和 CADisplayLink

    NSTimer *timer; CADisplayLink *caDisplayLink; int timeCount; - (void)viewDidLoad { [super viewDidLoa ...

  9. iOS三种定时器的用法NSTimer、CADisplayLink、GCD

    一,NSTimer //创建方式1 NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector: ...

随机推荐

  1. PHP 代理模式

    代理模式:为其他对象提供一种代理以控制对这个对象的访问. [代理模式中主要角色] 抽象主题角色:声明了代理主题和真实主题的公共接口,使任何需要真实主题的地方都能用代理主题代替. 代理主题角色:含有真实 ...

  2. [连载]《C#通讯(串口和网络)框架的设计与实现》- 14.序列号的设计,不重复的实现一机一码

    目       录 第十四章     序列号的设计... 2 14.1        设计原则... 2 14.2        设计思想... 3 14.3        代码实现... 4 14. ...

  3. ArcGIS Engine开发之书签加载

    ArcGIS中书签是保存特定视图范围的快捷方式.使用书签保存关注的视图范围,可在需要时快速定位.查看与浏览.书签功能主要用到IMapBookmarks.ISpatialBookmark和IAOIBoo ...

  4. Android XML中引用自定义内部类view的四个why

    今天碰到了在XML中应用以内部类形式定义的自定义view,结果遇到了一些坑.虽然通过看了一些前辈写的文章解决了这个问题,但是我看到的几篇都没有完整说清楚why,于是决定做这个总结. 使用自定义内部类v ...

  5. C# 知识特性 Attribute

    C#知识--获取特性 Attribute 特性提供功能强大的方法,用以将元数据或声明信息与代码(程序集.类型.方法.属性等)相关联.特性与程序实体关联后,可在运行时使用"反射"查询 ...

  6. AEAI WM V1.5.0 升级说明,开源工作管理系统

    1.升级说明 本次AEAI WM升级内容主要是针对数通畅联推出AEAI ECP企业云联平台而升级的,其中对AEAI WM的各模块进行扩展,同时增加了移动门户版功能及为AEAI ECP提供数据服务接口. ...

  7. Mac配置PHP

    前言 在MacOS中已经内置了PHP和Apache,所以不需要再额外安装它们,只需要简单几步即可运行PHP. 配置Apache 查看Apache版本: $ sudo apachectl -v 终端关闭 ...

  8. Windows Server 2012 虚拟化实战:SCVMM的安装和部署

    本篇大概介绍一下在Windows Server 2012 R2上安装和部署SCVMM的过程及其注意事项.下图是我们数据中心SCVMM的基本架构,其中 SCVMM Database 是用于存储了所有配置 ...

  9. x01.BSheepTree: 树

    数据结构,无外乎三: 1. 一对一,线性表,数组是也: 2. 一对多,树,菜单是也: 3. 多对多,图,网络是也. 涉及到树,有一个平衡的问题,左旋转,右旋转,转得人晕晕乎乎.好在陈广的<数据结 ...

  10. codegate-quals-2013-vuln100

    最近想多看看题目积累些经验, -------- 程序分析 64位,保护措施都没开 gdb-peda$ checksec CANARY : disabled FORTIFY : disabled NX ...