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: ...
随机推荐
- 让一个数组中存在N多个函数。让每个函数执行的 时候自动加1
function test(){ var arr = [ ]; for (var i = 0; i < 10; i++) { (function(i){ arr[i] = function(){ ...
- python之定义参数模块argparse(一)基本使用
在shell脚本中,若脚本带参数,则在脚本中使用$1.$2...等引用, 在python中,也可以定义类似的引用参数,可以为必选项也可以可选项. 基本用法如下三种: 1.必选项(位置参数) impor ...
- Java面向对象--类的对象之间的几种关系详解
转载: https://www.cnblogs.com/supren/p/7853377.html
- 11.Flask钩子函数
在Flask中钩子函数是使用特定的装饰器的函数.为什么叫做钩子函数呢,是因为钩子函数可以在正常执行的代码中,插入一段自己想要执行的代码,那么这种函数就叫做钩子函数. before_first_requ ...
- .NET Core玩转机器学习
最近在搞机器学习,目前国内没有什么关于ML.NET的教程,官方都是一大堆英文,经过了我的努力,找到了Relax Development大哥的博客,有关于ML.NET的内容 原文地址:https://w ...
- Filebeat 模块与配置
续 • <开始使用Filebeat> 1. 关于Filebeat 当你要面对成百上千.甚至成千上万的服务器.虚拟机和容器生成的日志时,请告别 SSH 吧!Filebeat 将为你提供一种 ...
- java基础(五)-----关键字static
在Java中并不存在全局变量的概念,但是我们可以通过static来实现一个“伪全局”的概念,在Java中static表示“全局”或者“静态”的意思,用来修饰成员变量和成员方法,当然也可以修饰代码块. ...
- Python爬虫入门教程 26-100 知乎文章图片爬取器之二
1. 知乎文章图片爬取器之二博客背景 昨天写了知乎文章图片爬取器的一部分代码,针对知乎问题的答案json进行了数据抓取,博客中出现了部分写死的内容,今天把那部分信息调整完毕,并且将图片下载完善到代码中 ...
- Kestrel.Transport.Sockets分析与使用
相信大家都清楚asp core有着非常出色的性能,它出色的性能也源于网络服务模块Kestrel:在techempower测试中Kestrel基础核心达到了700万级别的RPS吞吐能力,具备这样的能力那 ...
- Asp.Net SignalR 集群会遇到的问题
SignalR集群 当客户端数量上来,一台server自然是吃不消的.多个server集群部署是必然的解决方案.再通过负载均衡,嗯 简直是完美.但是问题也接踵而来.每个server只能管理到当前ser ...