让应用程序在执行代码之前等待几秒钟?

And*_*ers 7 ios

我试图实现一种在我的应用程序中截取屏幕截图的方法.我希望UINavigationBar提示向上滑动 - 截取屏幕截图 - 然后UINavigationBar可以轻松下滑.我需要应用程序在某些代码行之间等待/保持几秒钟,因为这样第一个动画没有时间完成:

[self.navigationController setNavigationBarHidden:YES animated:YES ];
[self.navigationController setNavigationBarHidden:NO animated:YES];
Run Code Online (Sandbox Code Playgroud)

那么,有没有一种延迟执行的方法,就像动画一样按钮这样:

[UIView animateWithDuration:0.5 delay:3 options:UIViewAnimationOptionCurveEaseOut animations:^{self.myButton.frame = someButtonFrane;} completion:nil];
Run Code Online (Sandbox Code Playgroud)

问候

vit*_*uha 11

您可以使用:

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 *
     ***********************/
});    
Run Code Online (Sandbox Code Playgroud)


nul*_*ull 3

您可以使用:

[self performSelector:@selector(hideShowBarButton) withObject:nil afterDelay:1.0];
Run Code Online (Sandbox Code Playgroud)

而且当然:

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