#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. [改善Java代码]三元操作符的类型务必一致

    建议三: 三元操作符是if-else的简化写法,在项目中使用它的地方很多,也非常好用,但是好用又简单的东西并不表示就可以随便用,我们来看看下面这段代码: public class Client { p ...

  2. Xcode磁盘空间大清理(转)

    Xcode磁盘空间大清理 我的设备是Macbook Air 13’ Mid 2011,128G SSD.最近开始有些存储压力了,用Clean My Mac清理一部分旧文件后,决定对Xcode动手. 移 ...

  3. Lombok(1.14.8) - @NoArgsConstructor, @RequiredArgsConstructor & @AllArgsConstructor

    @NoArgsConstructor @NoArgsConstructor,提供一个无参的构造方法. package com.huey.hello.bean; import java.util.Dat ...

  4. 解决MS Office下载网站数据失败的问题

    最近遇到在MS Excel中建立的Web Query在创建完成后过了一段时间(或关闭文件后再次打开文件并刷新数据)出现无法刷新的问题,点击刷新时报错如下: 无法下载您要求的信息. 这是一个很不友好的报 ...

  5. Android简单拨号

    package com.example.phonecall; import android.net.Uri; import android.os.Bundle; import android.app. ...

  6. 如何检查mysql中建立的索引是否生效的检测方法及相关参数说明

    所使用的mysql函数explain语法:explain < table_name >例如: explain select * from t3 where id=3952602;expla ...

  7. oracle储存过程,job,视图,触发器(记性不好,写个例子自己记)

    存储过程 create or replace procedure TestPro(Descerr out varchar2 ) is begin select * from test; excepti ...

  8. iOS开发——消息推送跳转

    项目开发用集成是极光推送JPush     这里主要是消息推送过来处理对应界面跳转          同时看到两篇写的不错的相关博客分享一下:      http://www.jianshu.com/ ...

  9. Objective-C 【点语法】

    ------------------------------------------- 点语法的使用 // //  点语法的使用 // //  点语法:   xcode的一种特性,xcode帮我们做代 ...

  10. select2 取值 赋值

    项目中心引入了select2的插件.优势:可以多选.搜索,缺点:存取值不如select方便. select2 取值: <script type="text/javascript&quo ...