美文网首页
定时器(转载)

定时器(转载)

作者: BLUEVIPIOS_ | 来源:发表于2017-11-24 13:32 被阅读0次

定时器方面碰到了一些坑,特做下记录。

一,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.执行一次

doubledelayInSeconds = 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)intcountDown;// 倒数计时用

@property(nonatomic,strong)NSDate *beforeDate;// 上次进入后台时间

@end

staticintconsttick = 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];

intinterval = (int)ceil([now timeIntervalSinceDate:_beforeDate]);

intval = _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];

}

}

相关文章

  • 定时器(转载)

    定时器方面碰到了一些坑,特做下记录。 一,NSTimer //创建方式1 NSTimer*timer=[NSTim...

  • 2017.12.21学习总结

    下午学习了定时器,定时器分为高级定时器、通用定时器和基本定时器,我们主要研究通用定时器。 定时器中断实现步骤:...

  • javascript笔记6

    定时器-间歇性定时器 定时器-延时定时器 认识DOM 间歇性定时器var time = window.setInt...

  • 定时器弹框、定时器基本用法、定时器动画、时钟

    定时器弹框: 定时器基本用法: 定时器动画: 时钟:

  • 无标题文章

    iOS NSTimer使用详解-开启、关闭、移除 定时器定时器详解ios定时器关闭定时器NSTimer 1、要使用...

  • iOS循环引用

    以下所有内容属笔者原创, 如有雷同纯属巧合, 未经允许不得转载. 这篇内容主要讲解 定时器 中的循环引用, 常见...

  • JDK Timer实现详解

    概述 本文首发于我的个人博客JDK Timer实现详解,严禁转载。定时器是工程开发中比较常用的工具,本文研究JDK...

  • 定时器

    1.倒计定时器(setTimeout) clearTimeout清除定时器2.循环定时器(setInterval)...

  • 第十三节 JavaScript 定时器 单线程

    一、定时器 1. JS存在两种定时器 setTimeout() 延迟定时器 setInterval() ...

  • 定时器 - OC

    定时器的定义 创建一个定时器并启动这个定时器 停止定时器 后续了解:NSTimer invalidate不起作用h...

网友评论

      本文标题:定时器(转载)

      本文链接:https://www.haomeiwen.com/subject/vxaxbxtx.html