Method1. Performselector method Method2. Nstimer Timer Method3. The sleepMethod4 of the nsthread thread. GCD
Public deferred execution methods
- (void)delayMethod{ NSLog(@"delayMethodEnd"); }
Method1:performselector
[self performSelector:@selector(delayMethod) withObject:nil/*可传任意类型参数*/ afterDelay:2.0];
Note: This method is a non-blocking execution, and no method is found to cancel execution.
Program Run End
2015-08-31 10:56:59.361 CJDelayMethod[1080:39604] delayMethodStart2015-08-31 10:56:59.363 CJDelayMethod[1080:39604] nextMethod2015-08-31 10:57:01.364 CJDelayMethod[1080:39604] delayMethodEnd
Method2:nstimer Timer
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(delayMethod) userInfo:nil repeats:NO];
Note: This method is a non-blocking execution mode,
Cancel execution method: - (void)invalidate; You can
Program Run End
2015-08-31 10:58:10.182 CJDelayMethod[1129:41106] delayMethodStart2015-08-31 10:58:10.183 CJDelayMethod[1129:41106] nextMethod2015-08-31 10:58:12.185 CJDelayMethod[1129:41106] delayMethodEnd
Method3:nsthread Thread of Sleep
[NSThread sleepForTimeInterval:2.0];
Note: This method is a blocking execution method that is recommended to be executed in a sub-thread, otherwise it will get stuck in the interface. However, sometimes it is necessary to block execution, such as entering the welcome interface needs to sleep 3 seconds before entering the main interface.
The cancel execution method was not found.
Program Run End
2015-08-31 10:58:41.501 CJDelayMethod[1153:41698] delayMethodStart2015-08-31 10:58:43.507 CJDelayMethod[1153:41698] nextMethod
Method4:gcd
__block ViewController/*主控制器*/ *weakSelf = self;
dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0/*延迟执行时间*/ * NSEC_PER_SEC));dispatch_after(delayTime, dispatch_get_main_queue(), ^{ [weakSelf delayMethod];});`
Note: This method can select the executing thread in the parameter, which is a non-blocking execution mode. The cancel execution method was not found.
Program Run End
2015-08-31 10:59:21.652 CJDelayMethod[1181:42438] delayMethodStart2015-08-31 10:59:21.653 CJDelayMethod[1181:42438] nextMethod2015-08-31 10:59:23.653 CJDelayMethod[1181:42438] delayMethodEnd
Complete code See:
//
Viewcontroller.m
Cjdelaymethod
//
Created by Chen Jie on 8/31/15.
Copyright (c) Chenjie. All rights reserved.
//
Import "ViewController.h"
@interface Viewcontroller ()
@property (nonatomic, Strong) Nstimertimer;
@end
@implementation Viewcontroller
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. NSLog(@"delayMethodStart"); [self methodOnePerformSelector];// [self methodTwoNSTimer];// [self methodThreeSleep];// [self methodFourGCD]; NSLog(@"nextMethod");}
- (void)methodFiveAnimation{ [UIView animateWithDuration:0 delay:2.0 options:UIViewAnimationOptionAllowUserInteraction animations:^{ } completion:^(BOOL finished) { [self delayMethod]; }];}
- (void)methodFourGCD{ __block ViewController weakSelf = self; dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)); dispatch_after(delayTime, dispatch_get_main_queue(), ^{ [weakSelf delayMethod]; });}
- (void)methodThreeSleep{ [NSThread sleepForTimeInterval:2.0];}
- (void)methodTwoNSTimer{ NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(delayMethod) userInfo:nil repeats:NO];}
- (void)methodOnePerformSelector{ [self performSelector:@selector(delayMethod) withObject:nil/*可传任意类型参数*/ afterDelay:2.0];}
- (void)delayMethod{ NSLog(@"delayMethodEnd");}
- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}
Text/Stupid Programming (Jane book author)
Original link: http://www.jianshu.com/p/6ed28a29b391
Copyright belongs to the author, please contact the author to obtain authorization, and Mark "book author".
IOS Delay Call