Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delay execution iOS app

I'm looking for a method that is an easy and straightforward way to delay execution for a number of frames in an iOS app, is this possible at all ?

like image 990
Balthatczar Avatar asked Dec 06 '22 07:12

Balthatczar


1 Answers

Depending on the complexity of what you're doing, this small snippet could suffice:

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
});

Otherwise you could look into NSTimer to schedule some code to be executed. You can even repeat once or many times.

Can be used like this for example:

[NSTimer scheduledTimerWithTimeInterval:5.0 
                                 target:self 
                               selector:@selector(myMethod) 
                               userInfo:nil 
                                repeats:NO];

For a great answer with a lot more info on how to use it, check out this post and of course, check out the documentation.

like image 106
Daniel Avatar answered Dec 28 '22 23:12

Daniel