一,NSTimer

//创建方式1
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(action:) userInfo:nil repeats:NO];
[timer invalidate];
//调用创建方法后,target对象的计数器会加1,直到执行完毕,自动减1。如果是循环执行的话,就必须手动关闭,否则可以不执行释放方法。 //推荐-->创建方式2
NSTimer *timer = [NSTimer timerWithTimeInterval:5 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
[timer invalidate];

存在延迟:不管是一次性的还是周期性的timer的实际触发事件的时间,都会与所加入的RunLoop和RunLoop Mode有关,如果此RunLoop正在执行一个连续性的运算,timer就会被延时出发。

二,CADisplayLink

- (void)startDisplayLink{
    self.displayLink = [CADisplayLink displayLinkWithTarget:self
                                                   selector:@selector(handleDisplayLink:)];
    [self.displayLink addToRunLoop:[NSRunLoop currentRunLoop]
                           forMode:NSDefaultRunLoopMode];
} - (void)handleDisplayLink:(CADisplayLink *)displayLink{
    //do something
} - (void)stopDisplayLink{
    [self.displayLink invalidate];
    self.displayLink = nil;
}

CADisplayLink是一个能让我们以和屏幕刷新率同步的频率将特定的内容画到屏幕上的定时器类。 CADisplayLink以特定模式注册到runloop后, 每当屏幕显示内容刷新结束的时候,runloop就会向 CADisplayLink指定的target发送一次指定的selector消息, CADisplayLink类对应的selector就会被调用一次。

iOS设备的屏幕刷新频率是固定的,CADisplayLink在正常情况下会在每次刷新结束都被调用,精确度相当高。使用场合相对专一,适合做UI的不停重绘,比如自定义动画引擎或者视频播放的渲染。不需要在格外关心屏幕的刷新频率了,本身就是跟屏幕刷新同步的。

三,GCD

1.执行一次

double delayInSeconds = 2.0;    

dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);   

 dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ 
          //执行事件     });

2.重复执行

NSTimeInterval period = 1.0; //设置时间间隔    

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);    dispatch_source_set_timer(_timer, dispatch_walltime(NULL, 0), period * NSEC_PER_SEC, 0); //每秒执行    

dispatch_source_set_event_handler(_timer, ^{    //在这里执行事件    });

    dispatch_resume(_timer);

GCD的定时器和NSTimer是不一样的,NSTimer受RunLoop影响,但是GCD的定时器不受影响,因为RunLoop也是基于GCD的


计时器(全局)

@interface ViewController ()
@property(nonatomic,strong)NSTimer *timer; // timer
@property(nonatomic,assign)int countDown; // 倒数计时用
@property(nonatomic,strong)NSDate *beforeDate; // 上次进入后台时间 @end static int const tick = 60; @implementation ViewController - (void)viewDidLoad {
    [super viewDidLoad];
    [self setupNotification];
    [self startCountDown];
} -(void)dealloc {
    [[NSNotificationCenter defaultCenter]removeObserver:self name:UIApplicationDidEnterBackgroundNotification object:nil];
    [[NSNotificationCenter defaultCenter]removeObserver:self name:UIApplicationWillEnterForegroundNotification object:nil];
    [self stopTimer];
} -(void)setupNotification {
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(enterBG) name:UIApplicationDidEnterBackgroundNotification object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(enterFG) name:UIApplicationWillEnterForegroundNotification object:nil];
} /**
 *  进入后台记录当前时间
 */
-(void)enterBG {
    NSLog(@"应用进入后台啦");
    _beforeDate = [NSDate date];
} /**
 *  返回前台时更新倒计时值
 */
-(void)enterFG {
    NSLog(@"应用将要进入到前台");
    NSDate * now = [NSDate date];
    int interval = (int)ceil([now timeIntervalSinceDate:_beforeDate]);
    int val = _countDown - interval;
    if(val > 1){
        _countDown -= interval;
    }else{
        _countDown = 1;
    }
} /**
 *  开始倒计时
 */
-(void)startCountDown {
    _countDown = tick; //< 重置计时
    _timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(timerFired:) userInfo:nil repeats:YES]; //< 需要加入手动RunLoop,需要注意的是在NSTimer工作期间self是被强引用的
    [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes]; //< 使用NSRunLoopCommonModes才能保证RunLoop切换模式时,NSTimer能正常工作。
} -(void)timerFired:(NSTimer *)timer {
    if (_countDown == 0) {
        [self stopTimer];
        NSLog(@"重新发送");
    }else{
        _countDown -=1;
        NSLog(@"倒计时中:%d",_countDown);
    }
} - (void)stopTimer {
    if (_timer) {
        [_timer invalidate];
    }
}

iOS三种定时器的用法NSTimer、CADisplayLink、GCD的更多相关文章

  1. iOS 计时器三种定时器的用法NSTimer、CADisplayLink、GCD

    原文:http://www.cocoachina.com/ios/20160919/17595.html DEMO链接

  2. NSTimer、CADisplayLink、GCD 三种定时器的用法 —— 昉

    在软件开发过程中,我们常常需要在某个时间后执行某个方法,或者是按照某个周期一直执行某个方法.在这个时候,我们就需要用到定时器. 在iOS中有很多方法完成定时器的任务,例如 NSTimer.CADisp ...

  3. iOS 三种定时器

    http://www.cocoachina.com/ios/20160905/17482.html

  4. Objective-C三种定时器CADisplayLink / NSTimer / GCD的使用

    OC中的三种定时器:CADisplayLink.NSTimer.GCD 我们先来看看CADiskplayLink, 点进头文件里面看看, 用注释来说明下 @interface CADisplayLin ...

  5. cocos2dx三种定时器使用

         cocos2dx三种定时器的使用以及停止schedule.scheduleUpdate.scheduleOnce 今天白白跟大家分享一下cocos2dx中定时器的用法. 首先,什么是定时 ...

  6. IOS三种归档(NSKeyArchieve)的总结

    IOS三种归档(NSKeyArchieve)的总结 归档是一种IOS中常用来存储文件的一种方法,在面向对象的语言中,归档也就实际上可以将一切对象存储在文件中,以下是IOS开发中常见的三种文件归档方式, ...

  7. C#中三种定时器对象的比较 【转】

    https://www.cnblogs.com/zxtceq/p/5667281.html C#中三种定时器对象的比较 ·关于C#中timer类 在C#里关于定时器类就有3个1.定义在System.W ...

  8. C#中三种定时器对象的比较

    ·关于C#中timer类 在C#里关于定时器类就有3个1.定义在System.Windows.Forms里2.定义在System.Threading.Timer类里3.定义在System.Timers ...

  9. iOS 三种录制视频方式

    随着每一代 iPhone 处理能力和相机硬件配置的提高,使用它来捕获视频也变得更加有意思.它们小巧,轻便,低调,而且与专业摄像机之间的差距已经变得非常小,小到在某些情况下,iPhone 可以真正替代它 ...

随机推荐

  1. C#调用Geocoding API进行地理编码与逆编码

    使用C#调用Geocoding API来将地址转为经纬度,或者将经纬度转变为具体的地址. Geocoding API的详细介绍参见:http://developer.baidu.com/map/web ...

  2. [swustoj 856] Huge Tree

    Huge Tree(0856) 问题描述 There are N trees in a forest. At first, each tree contains only one node as it ...

  3. MySQL索引的查看创建和删除

    1.索引作用 在索引列上,除了上面提到的有序查找之外,数据库利用各种各样的快速定位技术,能够大大提高查询效率.特别是当数据量非常大,查询涉及多个表时,使用索引往往能使查询速度加快成千上万倍. 例如,有 ...

  4. Centos 下Nginx 自启动脚本

    #!/bin/bash #ckconfig: NGINX_PATH=/web/container/nginx- NGINX_COMMAND=$NGINX_PATH/sbin/nginx NGINX_P ...

  5. Java中的字符串驻留(String Interning)

    1. 首先String不属于8种基本数据类型,String是一个对象. 因为对象的默认值是null,所以String的默认值也是null:但它又是一种特殊的对象,有其它对象没有的一些特性. 2. ne ...

  6. C++学习笔记:不用sizeof判断int类型占用几个字节

    #include <stdio.h> #include <string.h> char *change(int val, int base, char *retbuf) { s ...

  7. SMG12232ZK标准字符点阵型液晶显示模块的演示程序[C51编程语言][MCS51并行接口方式]

    //SMG12232ZK标准字符点阵型液晶显示模块的演示程序[C51编程语言][MCS51并行接口方式] //应用产品: SMG12232ZK标准中文字符点阵型液晶显示模块 // 本演示程序适用于SM ...

  8. 访问修饰符(access modifier)。

    1)public(公共的):被 public所修饰的属性和方法可以被有类访问. 所修饰的属性和方法可以被有类访问. 2)protected (受保护的):被protected所修饰的属性和方法可以在类 ...

  9. ubuntu 运行android sdk 下的工具adb报bash: ./adb: No such file or directory

    运行adb出现这种错误: bash: ./adb: No such file or directory   但adb确实存在. 可能1:你用的是64位的Linux,没装32位运行时库,安装 $ sud ...

  10. Java中finalize方法用途何在?

    package thinking.in.java.demo; /* * finalize的用途何在? * *本例的终止条件是L所有的Book对象在被当做垃圾回收前都应该被签入.但是在main方法中 * ...