#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的更多相关文章

  1. IOS中定时器NSTimer的开启与关闭

    调用一次计时器方法: myTimer = [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(scro ...

  2. 【转】iOS中定时器NSTimer的使用

    原文网址:http://www.cnblogs.com/zhulin/archive/2012/02/02/2335866.html 1.初始化 + (NSTimer *)timerWithTimeI ...

  3. iOS中定时器NSTimer的使用-备用

    1.初始化 + (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelect ...

  4. 【转】IOS中定时器NSTimer的开启与关闭

    原文网址:http://blog.csdn.net/enuola/article/details/8099461 调用一次计时器方法: myTimer = [NSTimer scheduledTime ...

  5. 【转】 IOS中定时器NSTimer的开启与关闭

    原文网址:http://blog.csdn.net/enuola/article/details/8099461 调用一次计时器方法: myTimer = [NSTimer scheduledTime ...

  6. 蜗牛爱课- iOS中定时器NSTimer使用

    调用一次计时器方法: //不重复,只调用一次.timer运行一次就会自动停止运行 self.locationTimer = [NSTimer  target:self selector: @selec ...

  7. iOS中定时器NSTimer的使用/开启与关闭

      一.只调用一次计时器方法: //不重复,只调用一次.timer运行一次就会自动停止运行 myTimer = [NSTimer scheduledTimerWithTimeInterval:1.5  ...

  8. iOS 中的 NSTimer

    iOS 中的 NSTimer NSTimer fire 我们先用 NSTimer 来做个简单的计时器,每隔5秒钟在控制台输出 Fire .比较想当然的做法是这样的: @interface Detail ...

  9. iOS中的NSTimer 和 Android 中的Timer

    首先看iOS的, Scheduling Timers in Run Loops A timer object can be registered in only one run loop at a t ...

随机推荐

  1. Spring 简单入门实例

    首先新建一个Web 项目 导入相应Jar 包 <?xml version="1.0" encoding="UTF-8"?> <beans xm ...

  2. 改变xmind显示中文界面

    最近打算迁移到ubuntu系统做为主系统,打算使用xmind这个工具,因为我的ubuntu并没有使用简体中文,xmind是根据系统的语种来自动选择的,所以也直接为英文界面,实在使用不习惯,查到了这篇文 ...

  3. JS轮播图

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  4. jquery 60秒倒计时

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  5. WDatePicker 屏蔽onchange事件的解决办法

    受下面文章的启发,使用DatePicker自带的年月日相关的change事件,可以“勉强”实现input控件的onchange(),直接上代码: 1.第一种方式:利用DatePicker提供的年.月. ...

  6. Google Protocol Buffer

    Google Protocol Buffer(protobuf)是一种高效且格式可扩展的编码结构化数据的方法.和JSON不同,protobuf支持混合二进制数据,它还有先进的和可扩展的模式支持.pro ...

  7. 合理使用mysql中的load data infile导入数据

    基本语法: load data  [low_priority] [local] infile 'file_name txt' [replace | ignore]into table tbl_name ...

  8. 第三十篇、iOS开发中常用的宏

    //字符串是否为空 #define kStringIsEmpty(str) ([str isKindOfClass:[NSNull class]] || str == nil || [str leng ...

  9. CSS的继承与优先级

    CSS样式继承性 body,div,p{} html文档可以上图的种种节点树的形式表示,css层叠样式表中的各元素也有这种对应关系 <body>是文档中最大的根节点,body中的所有元素都 ...

  10. easyui知识累计.递增.

    (001) 偶然发现 easyui 1.4.4 版本以下在使用easyloader时的一个bug(声明:只有在使用easyloader加载模块时有此问题) : (只测试过1.4.2, 1.4.3, 1 ...