zoukankan      html  css  js  c++  java
  • 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;
    }
    
    
    
    继续跟进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;

  • 相关阅读:
    单/多文档的窗体类属性修改(VC_MFC)
    RTTI 与消息机制(VC_MFC)
    对话框属性页(VC_MFC)
    插入符与路径(VC_MFC)
    重画控件与系统托盘图标编写(VC_MFC)
    颜色,文件和字体对话框(VC_MFC)
    ODBC 数据类型和API(VC)
    CTreeCtrl 和 CListCtrl 控件(VC_MFC)
    找回桌面清理向导清理多余IE图标
    win7任务栏缩略图消失的解决方法
  • 原文地址:https://www.cnblogs.com/jx66/p/6237135.html
Copyright © 2011-2022 走看看