第一种: 
用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 = ;
[theTimer addToRunLoop: [NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
} //停用
[theTimer invalidate];
theTimer = nil;

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

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

  1. CADisplayLink 及定时器的使用

    第一种: 用CADisplayLink可以实现不停重绘. 例子:   CADisplayLink* gameTimer; gameTimer = [CADisplayLink displayLinkW ...

  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. [Abp 源码分析]十一、权限验证

    0.简介 Abp 本身集成了一套权限验证体系,通过 ASP.NET Core 的过滤器与 Castle 的拦截器进行拦截请求,并进行权限验证.在 Abp 框架内部,权限分为两块,一个是功能(Featu ...

  2. BBS论坛(十九)

    19.1.cms轮播图管理页面布局 (1)cms/cms_base.html <li class="nav-group banner-manage"><a hre ...

  3. .NET Core实战项目之CMS 第十六章 用户登录及验证码功能实现

    前面为了方便我们只是简单实现了基本业务功能的增删改查,但是登录功能还没有实现,而登录又是系统所必须的,得益于 ASP.NET Core的可扩展性因此我们很容易实现我们的登录功能.今天我将带着大家一起来 ...

  4. nginx替换响应头(重点:如何在替换时加上if判断)

    在实现微信小程序内嵌非业务域名时,通过nginx做镜像网站绕过小程序业务域名检测,但有一些表单页面提交后会返回一个302状态,由响应头Location的值决定提交成功后的跳转地址.那么问题来了,这个地 ...

  5. 华为oj之字符串反转

    题目: 字符串反转 热度指数:4940 时间限制:1秒 空间限制:32768K 本题知识点: 字符串 题目描述 写出一个程序,接受一个字符串,然后输出该字符串反转后的字符串.例如: 输入描述: 输入N ...

  6. docker~在centos容器中安装新程序

    上一篇我们使用了阿里加速器安装了centos镜像,然后创建了一个新容器,运行了这个镜像,这一讲我们来为这个镜像添加一些应用程序,然后再保存容器,push容器到仓储,大家就可以直接pull我生产的容器了 ...

  7. JavaScript面向对象--多态

    一.多态的概念 相同的函数作用于不同的对象,会得到不同的结果,这就是多态. 二.如果不用多态,会怎么样? 这里有个浅显易懂的例子,定义一个函数叫makeSound,传入不同的对象,函数体里要写不同的情 ...

  8. spring security使用自定义登录界面后,不能返回到之前的请求界面的问题

    昨天因为集成spring security oauth2,所以对之前spring security的配置进行了一些修改,然后就导致登录后不能正确跳转回被拦截的页面,而是返回到localhost根目录. ...

  9. leetcode — populating-next-right-pointers-in-each-node

    /** * * Source : https://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node/ * Sou ...

  10. 一个emoji引发的一条血案:mysql存储emoji表情字符时报错解决

    以下是我插入一条带表情的数据到mysql后出现错误 2019-03-04 14:24:40,462 ERROR 2807 [-/139.199.27.244/-/2ms POST /api/activ ...