Blocks instead of performSelector:withObject:afterDelay: [duplicate]
There's no built-in way to do that, but it's not too bad to add via a category:
@implementation NSObject (PerformBlockAfterDelay)- (void)performBlock:(void (^)(void))block afterDelay:(NSTimeInterval)delay { block = [[block copy] autorelease]; [self performSelector:@selector(fireBlockAfterDelay:) withObject:block afterDelay:delay];}- (void)fireBlockAfterDelay:(void (^)(void))block { block();}@end
Credit to Mike Ash for the basic implementation.
Here's a simple technique, based on GCD, that I'm using:
void RunBlockAfterDelay(NSTimeInterval delay, void (^block)(void)){ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC*delay), dispatch_get_current_queue(), block);}
I'm not a GCD expert, and I'd be interested in comments on this solution.
Another way (perhaps the worst way to do this for many reasons) is:
[UIView animateWithDuration:0.0 delay:5.0 options:UIViewAnimationOptionAllowUserInteraction animations:^{} completion:^(BOOL finished) { //do stuff here}];