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 ?
-
It's not clear exactly what you are asking, but maybe `NSTimer scheduledTimerWithTimeInterval` suits your needs? – bengoesboom Jun 06 '13 at 18:38
-
Careful with your terminology, I think you may be coming from another platform when you talk about "delaying for a number of **frames**". What you mean is delaying for an interval in time. – Daniel Jun 06 '13 at 18:53
4 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.
If you mean by some constant time, the common way to do this is to use a NSTimer to fire at some time in the future, or to use dispatch_after(), which can dispatch a block to either the main queue or a background one at some time in the future.

- 40,852
- 12
- 92
- 138
David is correct. Here's some code for how I use NSTimer.
-(void)startAnimating {
[NSTimer scheduledTimerWithTimeInterval:.3 target:self selector:@selector(animateFirstSlide) userInfo:nil repeats:NO];
self.pageTurner = [NSTimer scheduledTimerWithTimeInterval:6.0 target:self selector:@selector(turnPages) userInfo:nil repeats:YES];
}
-(void)animateFirstSlide {
[self animateSlideAtIndex:0];
}
-(void)stopPageTurner {
[self.pageTurner invalidate];
}

- 1,322
- 9
- 25
-
Thank you all for your reply's, i have plenty to experiment with now. – Balthatczar Jun 06 '13 at 21:38
You can use [NSTimer scheduledTimerWithTimeInterval: target: selector:]
method or also you can use inbuilt method as wait(time_in_seconds);
.
Hope this will help you.

- 9,095
- 5
- 44
- 56

- 2,248
- 8
- 33
- 61