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 ?
Asked
Active
Viewed 3,568 times
4
-
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
10
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.
2
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.
David H
- 40,082
- 12
- 89
- 132
2
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];
}
JeffRegan
- 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