定时器、多线程

1、几种定时介绍

2、定时任务

  • 1)performSelector

    // 延时调用
    /*
        1.5 秒后自动调用 self 的 hideHUD 方法
    */
    [self performSelector:@selector(hideHUD) withObject:nil afterDelay:1.5];
    
    // 取消延时调用
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(hideHUD) object:nil];
  • 2)GCD

    // 多线程
    /*
        1.5 秒后自动执行 block 里面的代码
    */
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    
        self.hud.alpha = 0.0;
    });
  • 3)NSTimer

    // 定时器
    /*
        1.5 秒后自动调用 self 的 hideHUD 方法
    */
    [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(hideHUD) userInfo:nil repeats:NO];

猜你喜欢

转载自www.cnblogs.com/CH520/p/9478597.html