延迟执行的几种方法

第一种:

  [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秒,再把任务提交到队列中执行。

猜你喜欢

转载自www.cnblogs.com/dashengios/p/10409862.html