iOS 定时任务

  • 方法1 performSelector
// 2.0s后自动调用self的test方法
[self performSelector:@selector(test) withObject:nil afterDelay:2.0];
  • 方法2 GCD
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    // 2.0s后自动执行这个block里面的代码
    self.view.backgroundColor = [UIColor blueColor];
});
  • 方法3 NSTimer
// 2.0s后自动调用self的test方法
[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(test) userInfo:nil repeats:NO];

你可能感兴趣的:(iOS 定时任务)