Anders
Anders

Reputation: 729

Making the app wait a few seconds before executing code?

Im trying to implement a way of taking a screenshot in my application. I want the UINavigationBar tip slide up - take the screenshot - and then the UINavigationBar can slide down nice and easy. I need the app to wait/hold a few seconds between some lines of code, because this way the first animation does not get time to finish:

[self.navigationController setNavigationBarHidden:YES animated:YES ];
[self.navigationController setNavigationBarHidden:NO animated:YES];

So, is there a way of delaying execution, like when animation a button like so:

[UIView animateWithDuration:0.5 delay:3 options:UIViewAnimationOptionCurveEaseOut animations:^{self.myButton.frame = someButtonFrane;} completion:nil];

regards

Upvotes: 7

Views: 4073

Answers (3)

Tarek Hallak
Tarek Hallak

Reputation: 18470

You can use:

[self performSelector:@selector(hideShowBarButton) withObject:nil afterDelay:1.0];

and of course:

- (void) hideShowBarButton{
    if (self.navigationController.navigationBarHidden)
        [self.navigationController setNavigationBarHidden:NO animated:YES ];
    else
        [self.navigationController setNavigationBarHidden:YES animated:YES ];
}

Upvotes: 3

vitaluha
vitaluha

Reputation: 183

You can use:

double delayInSeconds = 2.0; // number of seconds to wait
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
    /***********************
     * Your code goes here *
     ***********************/
});    

Upvotes: 11

Dave
Dave

Reputation: 46249

While there doesn't appear to be a callback for setNavigationBarHidden's completion, it will take UINavigationControllerHideShowBarDuration seconds. So just use a NSTimer to delay it:

[NSTimer scheduledTimerWithTimeInterval:UINavigationControllerHideShowBarDuration target:self selector:@selector(myFunction:) userInfo:nil repeats:NO];

You may want to add a small amount to the delay as a fail-safe;

[NSTimer scheduledTimerWithTimeInterval:UINavigationControllerHideShowBarDuration+0.05 target:self selector:@selector(myFunction:) userInfo:nil repeats:NO];

See also this related question: UINavigationControoller - setNavigationBarHidden:animated: How to sync other animations

Upvotes: 0

Related Questions