美文网首页
iOS 定时器

iOS 定时器

作者: yxibng | 来源:发表于2018-12-22 22:21 被阅读0次

参考了iOS定时器,你真的会使用吗?

  1. NSTimer
  • 必须加入到Runloop中
  • 受Runloop影响,存在延时
  • 受Runloop的Mode的影响,如添加到了NSDefaultRunLoopMode模式,滑动的时候mode切换为UITrackingRunLoopMode,定时器会暂停执行
  1. dispatch_after, dispatch source timer
  • 不受Runloop的影响,精度很高
  • handler代码在子线程
  • 注意dispatch_walltime可以让计时器按照真实时间间隔进行计时
  • 创建的timer一定要有dispatch_suspenddispatch_source_cancel否则定时器将不执行
  1. CADisplayLink
  • 必须添加到runloop
  • 和屏幕刷新率同步的定时器
  • 适合做界面的不停重绘,比如视频播放的时候需要不停地获取下一帧用于界面渲染,或者做动画

example:

  1. NSTimer
-(void)createNSTimerAndFire
{
    NSMutableDictionary *dic = @{@"count":@10}.mutableCopy;
    NSTimer *timer =  [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(runTimer:) userInfo:dic repeats:YES];
    [timer fire];
}

-(void)createNSTimerAndAddToRunloop
{
    NSMutableDictionary *dic = @{@"count":@10}.mutableCopy;
    NSTimer *timer =  [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(runTimer:) userInfo:dic repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
}

- (void)runTimer:(NSTimer *)timer
{
    NSLog(@"timer = %@, userInfo = %@",timer,timer.userInfo);

    NSMutableDictionary *dic = timer.userInfo;
    
    NSNumber *count = dic[@"count"];
    
    if (count.integerValue <= 0) {
        [timer invalidate];
        return;
    }
    dic[@"count"] = @(count.integerValue-1);
}
  1. GCD
- (void)dispatch_delay {
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        //do stomething after 3.0secs
    });
}

- (void)createDispatchTimer
{
    __block int count = 10;
    dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_main_queue());
    dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, 1.0 * NSEC_PER_SEC, 0 * NSEC_PER_SEC);
    dispatch_source_set_event_handler(timer, ^{
        count--;
        NSLog(@"count--%d",count);
        
        if (count <= 0) {
            
            dispatch_async(dispatch_get_main_queue(), ^{
                //do something in the main thread
            });
            
            // time out, cancel timer
            dispatch_source_cancel(timer);
        }
    });
    dispatch_resume(timer);
}
  1. CADisplayLink
- (void)createDisplayLink {
    
    CADisplayLink *link = [CADisplayLink displayLinkWithTarget:self selector:@selector(displayLinkHandler:)];
    link.frameInterval = 1;
    //ios10.0以后使用 preferredFramesPerSecond
    [link addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
}

- (void)displayLinkHandler:(CADisplayLink *)sender
{
    NSLog(@"sender = %@",sender);
    
    [sender invalidate];
}

相关文章

  • 无标题文章

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

  • iOS进阶-谈谈定时器

    目录 iOS提供定时器API 定时器开发中的坑 一、 iOS提供定时器API 二、定时器开发中的坑 2.1、必须办...

  • 定时器

    定时器 兼容ios

  • iOS中的定时器

    点击这里>> cocoaChina: iOS中的定时器 iOS中定时器有三种,分别是NSTimer、CADispl...

  • GCD定时器使用

    iOS中的常用定时器分为这几类: NSTimer CADisplayLink GCD定时器 选择GCD定时器原因:...

  • iOS:NSTimer的几种创建方式

    在iOS开发中,经常会用到定时器,iOS中常用的定时器有三种:NSTimer,GCD,CADisplayLink。...

  • iOS Timer

    iOS开发中定时器经常会用到,iOS中常用的定时器有三种,分别是NSTime,CADisplayLink和GCD。...

  • 每日笔记

    1、通过safari打开网页 2、iOS的几种定时器及区别 iOS的几种定时器及区别 3、long long类型 ...

  • iOS三大定时器

    iOS开发中定时器经常会用到,iOS中常用的定时器有三种,分别是NSTime,CADisplayLink和GCD。...

  • 定时器的使用介绍

    iOS中的定时器大致分为这几类: NSTimer CADisplayLink GCD定时器 (一)NSTimer ...

网友评论

      本文标题:iOS 定时器

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