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

7

我正在尝试在我的应用程序中实现截屏的方式。我想让UINavigationBar提示滑动 - 截屏 - 然后UINavigationBar可以轻松地向下滑动。我需要应用程序在某些代码行之间等待/保持几秒钟,因为这样第一个动画就没有时间完成:

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

那么,是否有一种延迟执行的方法,例如当动画按钮时:

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

问候

3个回答

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 *
     ***********************/
});    

3

您可以使用:

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

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

0

虽然似乎没有 setNavigationBarHidden 完成后的回调,但它将花费 UINavigationControllerHideShowBarDuration 秒钟。因此,只需使用 NSTimer 来延迟它:

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

你可能想在延迟时间上增加一点作为故障保护措施;

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

还可以参考这个相关问题:UINavigationControoller - setNavigationBarHidden:animated: 如何同步其他动画效果


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接