延迟执行的几种方法

第一种:

  [self performSelector:@selector(run) withObject:nil afterDelay:2.0];

 

第二种:

  [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(run) userInfo:nil repeats:NO];

第三种:最好

  dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

 

        NSLog(@"--------delay-------");

    });

 

GCD中的时间单位是纳秒。

延迟执行的原理是:先等2秒,再把任务提交到队列中执行。

posted on 2019-02-20 23:13  大圣ios博客  阅读(209)  评论(0编辑  收藏  举报