0

我正在开发一个 ios 应用程序。我在视图上使用此代码,以便在加载时使活动指示器和黑色背景出现在整个视图的位置。

// hiding all the uioutlet in the view
sfondo.hidden=TRUE;
dalText.hidden=TRUE;
alText.hidden=TRUE;
titoloText.hidden=TRUE;

// generating the activity indicator
CGRect screenBound = [[UIScreen mainScreen] bounds];
CGSize screenSize = screenBound.size;
CGFloat screenWidth = screenSize.width;
CGFloat screenHeight = screenSize.height;
imgCaricamento = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
[imgCaricamento setCenter:CGPointMake(screenWidth/2.0, screenHeight/2.0)]; // I do this because I'm in landscape mode
[self.view addSubview:imgCaricamento]; // spinner is not visible until started
[imgCaricamento startAnimating]; // start!

现在的问题是我在开始更新之前启动了这段代码并做了几件事。发生的情况是,在启动此代码时,视图会冻结一两秒,只有在此延迟时间之后,活动才会出现。这一次足以向用户传达应用程序表现不佳的印象......我怎样才能让这段代码在之前和立即执行?

4

2 回答 2

2

您可能正在做一些阻塞主线程的事情,可能是下载图像、从磁盘读取某些内容或解析一些数据。主线程负责刷新 UI,当它被阻塞时,UI 冻结。您可以通过在后台在不同线程上进行处理来解决此问题。

于 2013-05-08T08:59:19.063 回答
1

除了可能会阻塞主线程从而阻止更新 UI 的事情之外,我会准备UIActivityIndicatorView内部viewWillAppear并在内部执行[imgCaricamento startAnimating];viewDidAppear或者在您开始更新过程的地方执行。

于 2013-05-08T09:08:52.543 回答