3
我试图在我的代码中启动延迟,以便在动作到达代码的那一部分时,只要计划了延迟,一切都会停止。我已经设置了时间延迟,这只是代码应该如何执行的问题。计划的时间延迟
这是我在我的项目已经采用延时:
NSDate *timeDelay = [NSDate dateWithTimeIntervalSinceNow:5];
[NSThread sleepUntilDate:timeDelay];
正如你所看到的,这个片段输入延迟5秒。我遇到的问题是,当我使用这些代码时,它并没有达到我期望的效果。下面是我试图运行函数:
- (IBAction)executeProgram
{
UIAlertView *delayAlert = [[UIAlertView alloc]
initWithTitle:@"Delay"
message:@"This message follows with a 5 second delay."
delegate:nil
cancelButtonTitle:nil
otherButtonTitles:nil, nil];
// Enable when time is working properly
[delayAlert show];
NSDate *timeDelay = [NSDate dateWithTimeIntervalSinceNow:5];
[NSThread sleepUntilDate:timeDelay];
// dismisses the alert
[delayAlert dismissWithClickedButtonIndex:0 animated:YES];
}
我希望这段代码首先,显示警报,等待5秒钟,然后再解除警报。但是,这并没有发生。相反,发生的是,当我点击按钮时,有五秒钟延迟第一个,然后弹出警报并几乎立即解散它自己。我已经尝试过使用另一个代码片断来延迟时间,同样的反应。
CFAbsoluteTime startTime = CFAbsoluteTimeGetCurrent();
for (int x = 0; x<=5000; x++)
{
NSLog(@"%i",x);
}
CFAbsoluteTime endTime = CFAbsoluteTimeGetCurrent();
CFAbsoluteTime elapsedTime = endTime - startTime;
我在做什么错在这里?为什么会在弹出警报之前执行延迟?
作为一个侧面说明,迫使用户在一段时间的警报视图盯着这样使一个糟糕的体验,并可能警告不要在HIG。 –
@joshcaswell这不是我计划发布到App Store的应用程序,而是我将用于个人使用的应用程序。 –
@JoshCaswell另一个问题是类似的,所以谢谢你指出我。我不知道它存在。 –