当我的应用程序正在加载某些屏幕时,我有代码显示带有活动轮的“正在加载...”视图。如果加载时间特别长(例如,超过 4 秒),那么我想显示一条附加消息,上面写着“抱歉,这需要很长时间,请耐心等待!” 我这样做的方法是在 4 秒延迟上使用 NSTimer,它调用一个方法来创建一个等待视图,然后将这个新视图覆盖在加载视图上,这样单词不会重叠。如果页面加载时间少于 4 秒,则加载视图被隐藏,等待视图永远不会被触发,用户继续他或她的快乐方式。
当我测试需要超过 4 秒的屏幕加载时,我似乎无法显示额外的视图。这是我的代码:
// this method is triggered elsewhere in my code
// right before the code to start loading a screen
- (void)showActivityViewer
{
tooLong = YES;
waitTimer = [NSTimer scheduledTimerWithTimeInterval:4.0
target:self
selector:@selector(createWaitAlert)
userInfo:nil
repeats:NO];
[[NSRunLoop currentRunLoop] addTimer: waitTimer forMode: NSDefaultRunLoopMode];
loadingView = [LoadingView createLoadingView:self.view.bounds.size.width :self.view.bounds.size.height];
[self.view addSubview: loadingView];
// this next line animates the activity wheel which is a subview of loadingView
[[[loadingView subviews] objectAtIndex:0] startAnimating];
}
- (void)createWaitAlert
{
[waitTimer invalidate];
if (tooLong)
{
UIView *waitView = [LoadingView createWaitView:self.view.bounds.size.width :self.view.bounds.size.height];
[self.view addSubview:waitView];
}
}
// this method gets triggered elsewhere in my code
// when the screen has finished loading and is ready to display
- (void)hideActivityViewer
{
tooLong = NO;
[[[loadingView subviews] objectAtIndex:0] stopAnimating];
[loadingView removeFromSuperview];
loadingView = nil;
}