zoukankan      html  css  js  c++  java
  • CADisplayLink以及定时器的使用

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

    - (CADisplayLink *)link {
        if (!_link) {
            // 创建定时器,一秒钟调用rotation方法60次
            _link = [CADisplayLink displayLinkWithTarget:self selector:@selector(rotation)];
            // 手动将定时器加入事件循环中
            // NSRunLoopCommonModes会使得RunLoop会随着界面切换扔继续使用,不然如果使用Default的话UI交互没问题,但滑动TableView就会出现不转问题,因为RunLoop模式改变会影响定时器调度
            [_link addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
        }
        return _link;
    }

    第二种:iOS–NSTimer和CADisplayLink的用法

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

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

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

    NSTimer 我们通常会用在背景计算,更新一些数值资料,而如果牵涉到画面的更新,动画过程的演变,我们通常会用CADisplayLink。 
    但是要使用CADisplayLink,需要加入QuartzCore.framework及#import

    @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 默认每秒运行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;

     https://blog.csdn.net/honey199396/article/details/51613436

  • 相关阅读:
    Android NestedScrolling与分发机制 二
    Android NestedScrolling与分发机制
    Android:View随手指移动
    开发错误记录5-Failed to sync Gradle project ‘HideTitleDemo’
    TouchSlop与VelocityTracker认识
    Android判断Touch为滑动事件还是操作控件
    Android-动画简介
    开发错误记录6----友盟社会化分享与支付宝-微信支付问题
    设计模式之抽象工厂模式
    面试题_抽象工厂,字符反串,冒泡与选择排序
  • 原文地址:https://www.cnblogs.com/jukaiit/p/8876695.html
Copyright © 2011-2022 走看看