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 ...
随机推荐
- CF 19D - Points 线段树套平衡树
题目在这: 给出三种操作: 1.增加点(x,y) 2.删除点(x,y) 3.询问在点(x,y)右上方的点,如果有相同,输出最左边的,如果还有相同,输出最低的那个点 分析: 线段树套平衡树. 我们先离散 ...
- JAVA网络编程常见问题
一. 网络程序运行过程中的常见异常及处理 第1个异常是 java.net.BindException:Address already in use: JVM_Bind. 该异常发生在服务器端进行new ...
- Java开发的一个简单截屏工具
//源代码 import java.awt.*;import java.awt.datatransfer.DataFlavor;import java.awt.datatransfer.Transfe ...
- Servlet & JSP - 转发与重定向的区别
本文转载自:http://blog.csdn.net/kobejayandy/article/details/13762043 转发 转发的原理,可以通过下图展示: 浏览器的请求发送给组件 1,组件 ...
- hiho欧拉路·二 --------- Fleury算法求欧拉路径
hiho欧拉路·二 分析: 小Ho:这种简单的谜题就交给我吧! 小Hi:真的没问题么? <10分钟过去> 小Ho:啊啊啊啊啊!搞不定啊!!!骨牌数量一多就乱了. 小Hi:哎,我就知道你会遇 ...
- response小结(二)——文件下载
我们先来看一个最简单的文件下载的例子: package com.yyz.response; import java.io.FileInputStream; import java.io.IOExcep ...
- 【SQL】关于存储过程调用过程中事务的点点滴滴
1.调用两个存储过程 ---------------------------------------------------------------- -- 表[dbo].[aaa_test]中[id ...
- ASP.NET下运用Memcached
对于大型网站的高并发,在ASP.NET网站下的session性能并不高,所以造成人们一种印象,大型WEB项目使用JAVA的错觉,致使很多人吐槽微 软不给力,其实这好比拉不出怪地球引力,本文介绍Memc ...
- OpenGL7-3快速绘制(索引方式)
代码下载#include "CELLWinApp.hpp"#include <gl/GLU.h>#include <assert.h>#include &l ...
- vs2010创建COM以及调用
1,创建COM组件 2,调用COM 3,MFC调用COM