performSelector方法spa
执行方法:线程
[self performSelector:<#(SEL)#> withObject:<#(id)#> afterDelay:<#(NSTimeInterval)#>];
3d
取消执行方法:code
[ NSObject cancelPreviousPerformRequestsWithTarget:<#(id)#> selector:<#(SEL)#> object:<#(id)#> ];//这里的方法是 NSObject的静态方法orm
特色:此方式要求必须在主线程中执行,不然无效。是一种非阻塞的执行方式。get
2. NSTimerit
执行方法:io
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:<#(NSTimeInterval)#> target:<#(id)#> selector:<#(SEL)#> userInfo:<#(id)#> repeats:<#(BOOL)#>];form
取消方法:thread
[timer invalidate];//这里的是减方法
特色:此方式要求必须在主线程中执行,不然无效。是一种非阻塞的执行方式。
3. NSThread
执行方法:
NSThread *thread = [[NSThread alloc]initWithTarget:<#(id)#> selector:<#(SEL)#> object:<#(id)#>];
[NSThread sleepForTimeInterval:<#(NSTimeInterval)#>];
[thread start];
特色:此方式在主线程和子线程中都可执行。是一种阻塞的执行方式,建议放到子线程中。
4. dispatch_after
double delayInSeconds = 2.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
[dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
// <#code to be executed on the main queue after delay#>
}) ];//敲击dispatch_after 代码会自动生成