How to Perform Segue With Delay
You can achieve that by calling:
let delayInSeconds = 0.8
let segueIdentifier = "your segue id"
DispatchQueue.main.asyncAfter(deadline: .now() + delayInSeconds) { [unowned self] in
self.performSegue(withIdentifier: segueIdentifier, sender: nil)
}
I think there is a better approach which lets the controller itself to handle dispatching issues. You can achieve it like this:
first create a method like this which later you will use its selector
- (void)showAnotherViewController{
[self performSegueWithIdentifier:@"yourSegueToAnotherViewController" sender:self];
}
Then when you want to show another view controller after delay use this line of code inside your current view controller:
[self performSelector:@selector(showAnotherViewController) withObject:nil afterDelay:yourDelayInSeconds];
You can use GCD's dispatch_after
to execute your segue code 2 seconds after the view appears, e.x:
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
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){
[self performSegueWithIdentifier:@"splashScreenSegue" sender:self];
});
}
Additionally, please make sure that you remember to call the super implementation when overriding UIViewController's life cycle methods.