zoukankan      html  css  js  c++  java
  • IOS NSTimer和CADisplayLink的用法

    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;

     

     

    您的资助是我最大的动力!
    金额随意,欢迎来赏!

    如果,您认为阅读这篇博客让您有些收获,不妨点击一下右下角的推荐按钮。
    如果,您希望更容易地发现我的新博客,不妨点击一下绿色通道的关注我

    如果,想给予我更多的鼓励,求打

    因为,我的写作热情也离不开您的肯定支持,感谢您的阅读,我是【往事亦如风】!

  • 相关阅读:
    @Autowired和@Resource注解的注入顺序
    八大排序算法(Java实现)+ 自制动画
    一文秒杀三个经典面试求和问题
    这篇文章,或许对还在上学的你,有一些帮助
    得了,一文把前缀和给扒的干干净净了。
    BF,BM,KMP,就这?
    那个贼贵的比特币到底是什么原理?
    小样?别以为你穿了几个马甲就不认得你是二分法!
    面试前必知必会的二分查找及其变种
    Vue.js 官方示例初探
  • 原文地址:https://www.cnblogs.com/ldnh/p/5263368.html
Copyright © 2011-2022 走看看