这里列举了四种线程延时加载的方法,

1.performSelector方法 此方法必须在主线程中执行,并不是阻塞当前的线程

 

[self performSelector:@selector(delayMethod) withObject:nil afterDelay:1.0f];

2.定时器:NSTimer,也必须在主线程中加载,是一种非阻塞的执行方式

[NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(delayMethod) userInfo:nil repeats:NO];

3. sleep方式此方式在主线程和子线程中均可执行。 是一种阻塞的执行方式,建方放到子线程中,以免卡住界面

[NSThread sleepForTimeInterval:1.0f]; [self delayMethod];

4.GCD方式此方式在可以在参数中选择执行的线程。是一种非阻塞的执行方式

 

  //<#delayInSeconds#> 延迟的时间

  dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(<#delayInSeconds#> * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

        });








相关文章:

  • 2022-12-23
  • 2021-12-04
  • 2021-11-29
  • 2022-02-09
  • 2022-03-04
  • 2021-11-06
猜你喜欢
  • 2021-12-02
  • 2021-09-13
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案