timer

作者: tinghua | 来源:发表于2016-05-10 18:36 被阅读33次
    • 1.第一种 每一秒执行一次(重复性)
      double delayInSeconds = 1.0;
      timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0));
      dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC, 0.0);
      dispatch_source_set_event_handler(timer, ^{
      NSLog(@"timer date 1== %@",[NSDate date]);
      });
      dispatch_resume(timer);

    • 2.第二种 二秒后执行 (一次性)
      double delayInSeconds = 2.0;
      dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
      dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
      NSLog(@"timer date 2== %@",[NSDate date]);
      });

    • 3.第三种 每一秒执行一次 (重复性)
      [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(testTimer) userInfo:nil repeats:YES];

    相关文章

      网友评论

          本文标题:timer

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