简体   繁体   English

取消视图前,暂停应用以显示消息一秒钟

[英]Pausing app to display message for one second before dismissing a view

I have a simple timer that on completion shows the message "Rest Time Complete" I want that message to stay on the screen for one second, then dismiss a view. 我有一个简单的计时器,该计时器在完成时显示消息“ Rest Time Complete”(Rest Time Complete),我希望该消息在屏幕上停留一秒钟,然后关闭视图。 I've tried a few things that I'll detail below, but I can never get the message to display, pause, then dismiss the view. 我已经尝试了一些我将在下面详细介绍的事情,但是我永远无法使该消息显示,暂停然后关闭该视图。 The timer pauses on the last number in the timer, then the message always appears as the view is being dismissed. 计时器停在计时器中的最后一个数字上,然后在关闭视图时始终显示该消息。 Here is my code: 这是我的代码:

- (void)restTimerComplete
{
    self.restTimeLabel.text = @"Rest Period Complete!";
    [NSThread sleepForTimeInterval:1];
    [self hideRestTimerAnimated:YES];
}

I've also tried using the simple sleep(1) , but that has the same end result. 我也尝试过使用简单的sleep(1) ,但最终结果相同。

The simplest approach is to use dispatch_after : 最简单的方法是使用dispatch_after

- (void)restTimerComplete {
    self.restTimeLabel.text = @"Rest Period Complete!";
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self hideRestTimerAnimated:YES];
    });

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM