IOS development-delayed execution

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.