CADisplayLink 及定时器的使用
第一种:
用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;
}
-(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];//添加到循环并启动
}
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 及定时器的使用的更多相关文章
- CADisplayLink以及定时器的使用
第一种: 用CADisplayLink可以实现不停重绘. - (CADisplayLink *)link { if (!_link) { // 创建定时器,一秒钟调用rotation方法60次 _li ...
- iOS 音乐
iOS 音乐 这篇博客介绍的是较长的音频播放-音乐... 有关音效的介绍可以点击下面的连接进入... iOS 音效 首先导入框架:AVFoundation.framework 导入框架后我们在需要使用 ...
- iOS 水波效果
将水波效果放在子视图上,主控制器只负责加载 #import "WaveProgress.h" @interface WaveProgress () @property (nonat ...
- iOS开发Quartz2D之 七:雪花效果
#import "VCView.h" @implementation VCView -(void)awakeFromNib { //[NSTimer scheduledTimerW ...
- Objective-C三种定时器CADisplayLink / NSTimer / GCD的使用
OC中的三种定时器:CADisplayLink.NSTimer.GCD 我们先来看看CADiskplayLink, 点进头文件里面看看, 用注释来说明下 @interface CADisplayLin ...
- iOS CADisplayLink 定时器的使用
CADisplayLink 是一个能让我们以和屏幕刷新频率相同的频率将内容刻画到屏幕上的定时器,在应用中创建一个新的CADisplayLink对象,把他添加到一个runloop中,并且给他提供一个ta ...
- 运用CADisplayLink来开启定时器
CADisplayLink来开启定时器 CADisplayLink是以屏幕刷新频率将内容绘制到屏幕上的定时器,每秒60Hz.使用的时候,先创建一个CADisplayLink对象,将其添加到一个RunL ...
- 定时器 NSTimer 和 CADisplayLink
NSTimer *timer; CADisplayLink *caDisplayLink; int timeCount; - (void)viewDidLoad { [super viewDidLoa ...
- iOS三种定时器的用法NSTimer、CADisplayLink、GCD
一,NSTimer //创建方式1 NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector: ...
随机推荐
- 超小Web手势库AlloyFinger原理
目前AlloyFinger作为腾讯手机QQ web手势解决方案,在各大项目中都发挥着作用. 感兴趣的同学可以去Github看看:https://github.com/AlloyTeam/AlloyFi ...
- Android之RecyclerView的原生Bug-Inconsistency detected. Invalid view holder adapter positionViewHolder{a1bbfa3 position=2 id=-1, oldPos=-1, pLpos:-1 no parent}
今天在运行自己编写的App时,突然发现App在运行时闪退,然后就查看了Android Studio的Log,发现了这个错误,上网查了一下,才知道是RecyclerView的原生Bug,在数据更新时会出 ...
- android 之 启动画面的两种方法
现在,当我们打开任意的一个app时,其中的大部分都会显示一个启动界面,展示本公司的logo和当前的版本,有的则直接把广告放到了上面.启动画面的可以分为两种设置方式:一种是两个Activity实现,和一 ...
- 【转】最流行的编程语言JavaScript能做什么?
本文转自互联网! 首先很遗憾的一点是,“PHP虽然是最好的语言”,但是它不是最流行的语言. 对不起的还有刚刚在4月TIOBE编程语言排行榜上榜的各个语言: 你们都很棒,但是你们都担当不了这个大任. 开 ...
- Atitit linux获取项目运行环境版本
Atitit linux获取项目运行环境版本 1.1. Nginx版本1 1.2. Php版本1 1.3. Mysql版本2 1.4. Redis版本2 1.1. Nginx版本 [root@iZ25 ...
- Oracle update和order by
今天遇到一个关于SQL转换成Oracle语句的问题,描述如下: select * from emp order by deptno; select * from dept; Sql Server: u ...
- uploadify插件Http Error(302)错误记录(MVC)
由于项目(asp.net MVC)需要做一个附件上传的功能,使用的是jQuery的Uploadify插件的2.1.0版本,上传文件到自己项目指定的文件夹下面.做完之后,在谷歌上测试是正确的,在火狐上报 ...
- MongoDB学习笔记系列
回到占占推荐博客索引 该来的总会来的,Ef,Redis,MVC甚至Sqlserver都有了自己的系列,MongoDB没有理由不去整理一下,这个系列都是平时在项目开发时总结出来的,希望可以为各位一些帮助 ...
- 时隔一年再读到the star
The Star Arthur C. Clarke It is three thousand light-years to the Vatican. Once, I believed that spa ...
- 负margin的移位参考线
同早年~ 问题描述 在xx项目中,羊城通卡号的输入框处使用了xx库中的实现方式,即将提示文字标签<label>通过负margin移位到<input>框的下面.静态时展现良好,j ...