ios 中定时器:NSTimer, CADisplayLink, GCD
#import "ViewController.h"
#import "RunloopViewController.h"
@interface ViewController () @property (nonatomic , assign) NSInteger currentIndex; @property (nonatomic) CADisplayLink * timerInC; @property (nonatomic) UIImageView * imgV; @property (nonatomic) NSTimer * timerInN; @property (nonatomic) dispatch_source_t timerInG; @property (nonatomic , assign) BOOL nsTimerResume; @property (nonatomic , assign) BOOL gcdTimerResume; @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; self.view.backgroundColor = [UIColor grayColor]; self.imgV = [[UIImageView alloc] initWithFrame:CGRectMake(, , , )];
self.imgV.contentMode = UIViewContentModeScaleAspectFill;
self.imgV.center = self.view.center;
[self.view addSubview:self.imgV]; UIButton * button = [UIButton buttonWithType:(UIButtonTypeSystem)];
[button setFrame:CGRectMake(, , , )];
button.center = CGPointMake(self.view.center.x - , self.view.center.y + );
[self.view addSubview:button];
[button setTitle:@"CADisplayLink" forState:(UIControlStateNormal)];
[button setBackgroundColor:[UIColor whiteColor]];
[button addTarget:self action:@selector(CADisplayLinkAction) forControlEvents:(UIControlEventTouchUpInside)]; UIButton * button1 = [UIButton buttonWithType:(UIButtonTypeSystem)];
[button1 setFrame:CGRectMake(, , , )];
button1.center = CGPointMake(self.view.center.x, self.view.center.y + );
[self.view addSubview:button1];
[button1 setTitle:@"NSTimer" forState:(UIControlStateNormal)];
[button1 setBackgroundColor:[UIColor whiteColor]];
[button1 addTarget:self action:@selector(NSTimerAction) forControlEvents:(UIControlEventTouchUpInside)]; UIButton * button2 = [UIButton buttonWithType:(UIButtonTypeSystem)];
[button2 setFrame:CGRectMake(, , , )];
button2.center = CGPointMake(self.view.center.x + , self.view.center.y + );
[self.view addSubview:button2];
[button2 setTitle:@"GCDTimer" forState:(UIControlStateNormal)];
[button2 setBackgroundColor:[UIColor whiteColor]];
[button2 addTarget:self action:@selector(GCDTimerAction) forControlEvents:(UIControlEventTouchUpInside)]; UIButton * button3 = [UIButton buttonWithType:(UIButtonTypeSystem)];
[button3 setFrame:CGRectMake(, , , )];
button3.center = CGPointMake(self.view.center.x, self.view.center.y + );
[self.view addSubview:button3];
[button3 setTitle:@"看看Runloop" forState:(UIControlStateNormal)];
[button3 setBackgroundColor:[UIColor whiteColor]];
[button3 addTarget:self action:@selector(gotoRunloopAction) forControlEvents:(UIControlEventTouchUpInside)];
} -(void)viewWillAppear:(BOOL)animated
{
[self initTimer];
} -(void)initTimer
{
///target selector 模式初始化一个实例
self.timerInC = [CADisplayLink displayLinkWithTarget:self selector:@selector(changeImg)];
///暂停
self.timerInC.paused = YES;
///selector触发间隔
self.timerInC.frameInterval = ;
///加入一个runLoop
[self.timerInC addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
self.timerInN = [NSTimer timerWithTimeInterval:0.032 target:self selector:@selector(changeImg) userInfo:nil repeats:YES];
self.timerInN.fireDate = [NSDate distantFuture];
self.nsTimerResume = YES;
[[NSRunLoop currentRunLoop] addTimer:self.timerInN forMode:NSDefaultRunLoopMode];
self.gcdTimerResume = YES;
} -(void)changeImg
{
self.currentIndex ++;
if (self.currentIndex > ) {
self.currentIndex = ;
}
self.imgV.image = [UIImage imageNamed:[NSString stringWithFormat:@"%ld.jpg",self.currentIndex]];
} -(void)CADisplayLinkAction
{
self.nsTimerResume = YES;
self.timerInN.fireDate = [NSDate distantFuture];
if (self.timerInG && !self.gcdTimerResume) {
dispatch_suspend(self.timerInG);
self.gcdTimerResume = YES;
}
self.timerInC.paused = !self.timerInC.paused;
} -(void)NSTimerAction
{
self.timerInC.paused = YES; if (self.timerInG && !self.gcdTimerResume) {
dispatch_suspend(self.timerInG);
self.gcdTimerResume = YES;
}
self.timerInN.fireDate = self.nsTimerResume?
[NSDate distantPast]:[NSDate distantFuture];
self.nsTimerResume = !self.nsTimerResume;
} -(void)GCDTimerAction
{
if (self.gcdTimerResume) {
self.timerInC.paused = YES;
self.nsTimerResume = YES;
self.timerInN.fireDate = [NSDate distantFuture];
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
self.timerInG = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, , , dispatch_get_main_queue());
dispatch_source_set_timer(self.timerInG, dispatch_walltime(NULL, * NSEC_PER_SEC), 0.032 * NSEC_PER_SEC, );
dispatch_source_set_event_handler(self.timerInG, ^{
[self changeImg];
});
});
dispatch_resume(self.timerInG);
}
else
{
dispatch_suspend(self.timerInG);
}
self.gcdTimerResume = !self.gcdTimerResume;
} - (void)gotoRunloopAction
{
[self.timerInC invalidate];
self.timerInC = nil;
[self.timerInN invalidate];
self.timerInN = nil;
if (self.timerInG) {
if (self.gcdTimerResume) {
dispatch_resume(self.timerInG);
}
dispatch_source_cancel(self.timerInG);
self.timerInG = nil;
}
RunloopViewController * vc = [[RunloopViewController alloc] init];
[self.navigationController pushViewController:vc animated:YES];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end
ios 中定时器:NSTimer, CADisplayLink, GCD的更多相关文章
- IOS中定时器NSTimer的开启与关闭
		
调用一次计时器方法: myTimer = [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(scro ...
 - 【转】iOS中定时器NSTimer的使用
		
原文网址:http://www.cnblogs.com/zhulin/archive/2012/02/02/2335866.html 1.初始化 + (NSTimer *)timerWithTimeI ...
 - iOS中定时器NSTimer的使用-备用
		
1.初始化 + (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelect ...
 - 【转】IOS中定时器NSTimer的开启与关闭
		
原文网址:http://blog.csdn.net/enuola/article/details/8099461 调用一次计时器方法: myTimer = [NSTimer scheduledTime ...
 - 【转】 IOS中定时器NSTimer的开启与关闭
		
原文网址:http://blog.csdn.net/enuola/article/details/8099461 调用一次计时器方法: myTimer = [NSTimer scheduledTime ...
 - 蜗牛爱课- iOS中定时器NSTimer使用
		
调用一次计时器方法: //不重复,只调用一次.timer运行一次就会自动停止运行 self.locationTimer = [NSTimer target:self selector: @selec ...
 - iOS中定时器NSTimer的使用/开启与关闭
		
一.只调用一次计时器方法: //不重复,只调用一次.timer运行一次就会自动停止运行 myTimer = [NSTimer scheduledTimerWithTimeInterval:1.5 ...
 - iOS 中的 NSTimer
		
iOS 中的 NSTimer NSTimer fire 我们先用 NSTimer 来做个简单的计时器,每隔5秒钟在控制台输出 Fire .比较想当然的做法是这样的: @interface Detail ...
 - iOS中的NSTimer 和 Android 中的Timer
		
首先看iOS的, Scheduling Timers in Run Loops A timer object can be registered in only one run loop at a t ...
 
随机推荐
- 【解决】应用程序无法正常启动(0xc000007b)。请单击“确定”关闭应用程序。
			
换了SSD硬盘,装了Windows 7 SP1 x64的系统.用了一段时间,突然一天有些软件打不开了.弹出下面的提示 应用程序无法正常启动(0xc000007b).请单击“确定”关闭应用程序.第一时间 ...
 - <转>HTML+CSS总结/深入理解CSS盒子模型
			
原文地址:http://www.chinaz.com/design/2010/1229/151993.shtml 前言:前阵子在做一个项目时,在页面布局方面遇到了一点小问题,于是上stackoverf ...
 - 初识Less(2015年05月23日)
			
因为最近在研究Bootstrap,然后才了解到Less,听说Less很强大,又听说Bootstrap+Less会更搭,所以就决定也顺带了解下Less的相关知识. come on...... 一.简介 ...
 - MySQL数据库的存储结构
			
--把若干条sql语句封装起来,起个名字,叫做过程,也是没有返回值的函数 --把这个过程存储在数据库中->存储过程 --存储过程的创建过程 create procedure proceduceN ...
 - DOS批处理命令-pause
			
“pause”用于暂停批处理的执行器并给出提示信息,然后由用户决定是继续执行还是终端执行. 语法: pause 简单一个pause,不附带任何参数. 执行pause以后会提示一句话[按任意键继续执行] ...
 - C#中调用unmanaged DLL
			
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
 - 使用DNSSCrypt解决DNS污染问题
			
本文转自 月光博客,如有需要,请阅读原文. google近期在国内是不能访问了,dropbox这货居然也被DNS污染了,幸好发现DNSCrypt这一神器,防止DNS污染的绝佳工具. 基本原理:DNSC ...
 - C# 代理HTTP请求
			
目的: 应该有不少人需要去某些网站不停爬取数据,有时会使用HTTPRequest一直请求某个网站的某个网址.有的网站比如 QQ空间,赶集网(这是我测试的网站),不停刷新会提醒你的账号异常,可能会查封你 ...
 - C++对象的JSON序列化与反序列化探索完结-列表的序列化与反序列化
			
在前两篇文章中,我们已经完成对普通对象以及复杂对象嵌套的序列化与反序列化,见如下地址: C++对象的JSON序列化与反序列化探索 C++对象的JSON序列化与反序列化探索续-复杂对象的序列化与反序列化 ...
 - 使用MySQL数据库将汉字转换成拼音的一个C语言小程序
			
环境: mysql:mysql-5.1.65 centos:centos 6.5 编译命令: gcc -o chinesetopinyin chinesetopinyin.c -L/usr/lib/m ...