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 = ;
[theTimer addToRunLoop: [NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
} //停用
[theTimer invalidate];
theTimer = nil;
https://blog.csdn.net/honey199396/article/details/51613436
CADisplayLink以及定时器的使用的更多相关文章
- CADisplayLink 及定时器的使用
第一种: 用CADisplayLink可以实现不停重绘. 例子: CADisplayLink* gameTimer; gameTimer = [CADisplayLink displayLinkW ...
- 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: ...
随机推荐
- 关于移动端开发,vedio标签层级高遮挡蒙版的解决方案
问题描述: 使用famework7框架搭建了一个界面,然后再界面中需要使用蒙版效果,在PC端,ios测试没有问题,在Andriod播放视屏再点击显示蒙版的效果师,视频会遮盖蒙版.修改定位,z-inde ...
- mogodb分片配置
下图展示了在MongoDB中使用分片集群结构分布: 上图中主要有如下所述三个主要组件: Shard: 用于存储实际的数据块,实际生产环境中一个shard server角色可由几台机器组个一个repli ...
- Spring中你可能不知道的事(二)
在上一节中,我介绍了Spring中极为重要的BeanPostProcessor BeanFactoryPostProcessor Import ImportSelector,还介绍了一些其他的零碎知识 ...
- 使用xUnit为.net core程序进行单元测试 -- Assert
第一部分: http://www.cnblogs.com/cgzl/p/8283610.html Assert Assert做什么?Assert基于代码的返回值.对象的最终状态.事件是否发生等情况来评 ...
- C++版 - 剑指offer之面试题37:两个链表的第一个公共结点[LeetCode 160] 解题报告
剑指offer之面试题37 两个链表的第一个公共结点 提交网址: http://www.nowcoder.com/practice/6ab1d9a29e88450685099d45c9e31e46?t ...
- [Leetcode]237. Delete Node in a Linked List -David_Lin
Write a function to delete a node (except the tail) in a singly linked list, given only access to th ...
- 使用mpvue开发小程序教程(四)
在上一章节中,我们将vue-cli命令行工具生成的代码骨架中的src目录清理了一遍,然后从头开始配置和编写了一个可以运行的小程序页面,算是正真走上了使用mpvue开发小程序的第一步.今天我们将进一步来 ...
- 全文检索-Elasticsearch (二) CURD
ElasticSearch6.0以上版本的增删改查基本操作基于JSON的REST API与ElasticSearch进行通信.可以使用任何HTTP客户端来通信.当然ElasticSearch自己的文档 ...
- NTP服务器搭建
NTP服务器搭建 :http://www.jbxue.com/LINUXjishu/22352.html 客户端配置: vim /etc/ntp.conf #server 0.centos.pool. ...
- SQL优化总结之一
一.实践中如何优化mysql 1) SQL语句及索引的优化 2) 数据库表结构的优化 3) 系统配置的优化 4) 硬件优化 二.索引的底层实现原理和优化 2.1 底层实现 在DB2数据库中索引采用的是 ...