Sometimes, if you want to run a piece of code after a certain period of time, you need to use delayed execution.
There are several common methods:
1. The most direct method is performSelector: withObject: afterDelay: the disadvantage of this method: Write a method for the latency each time.
[self performSelector:@selector(scale_2) withObject:nil afterDelay:0.5f];
-(void)scale_2{ UIImageView *round_2 = [[UIImageView alloc]initWithFrame:CGRectMake(105, 210, 20, 20)]; round_2.image = [UIImage imageNamed:@"round_"]; [splashView addSubview:round_2]; [self setAnimation:round_2];}
2. Use category and use BOLCK to execute
@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
3. Use GCD [Code] c #/cpp/oc code:
void RunBlockAfterDelay(NSTimeInterval delay, void (^block)(void)){ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC*delay), dispatch_get_current_queue(), block);}
Poolo: note that the dispatch_getcurrent_queue () method in the figure has been kill in ios6. Now the first parameter of dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_HIGH, 0) is priority, the second parameter is 0 or nil if you want to operate on the interface, use dispatch_get_main_queue (); reference: http://www.cnblogs.com/guwandong/archive/2012/08/08/2626211.html 4. this may not be a good method. Use the [Code] c #/cpp/oc code of the completion parameter of animation:
[UIView animateWithDuration:0.0 delay:5.0 options:UIViewAnimationOptionAllowUserInteraction animations:^{} completion:^(BOOL finished) { //do stuff here}];
5. Use NSOperationQueue to run in the next main loop of the application: [Code] c #/cpp/oc code:
1 |
[[NSOperationQueue mainQueue] addOperationWithBlock:aBlock]; |
This is equivalent to calling mongomselector: with afterDelay of 0.0f.
On the way to learning, I will share with you.