因此,我试图在我的应用程序中创建一个加载屏幕。我有一个视图,需要3-10秒才能加载。在此期间,我想显示我制作的UIView,它是黑色的,带有一个加载屏幕。目前,我正在将我的代码放在viewDidLoad函数中,就在超级viewDidLoad之后。这是我的密码
UIView* baseView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
[self.view addSubview:baseView];
[baseView setBackgroundColor:[UIColor blackColor]];
[self.view bringSubviewToFront:baseView];
baseView.layer.zPosition = 1; 这起作用,并创建了我的视图之上的一切,我完全想要它,但这等待直到我的主视图完全完成加载,然后它实际显示任何东西。viewDidLoad不是一个放置这个的好地方,如果是的话,我应该把它放在哪里。
简单地说,我有一个非常基本的UIView,我希望在等待实际视图加载时加载它,然后简单地隐藏它。有什么办法吗?
发布于 2015-04-27 02:07:25
您需要在另一个线程上分派您的辛勤工作,否则操作系统将等到所有进程完成后才刷新UI (这就是为什么只在3-10秒之后才会看到加载屏幕)。
别忘了在长时间的工作完成后返回主线程。所有UI更新都必须在主线程上完成。
self.loadingView.hidden = NO;
// Dispatch lengthy stuff on another thread
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
// Do lengthy stuff here
// Dispatch back on the main thread (mandatory for UI updates)
dispatch_async(dispatch_get_main_queue(), ^{
self.loadingView.hidden = YES;
});
});发布于 2015-04-27 04:11:56
UIView* baseView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
[self.view addSubview:baseView];
[baseView setBackgroundColor:[UIColor blackColor]];
[self.view bringSubviewToFront:baseView];
baseView.layer.zPosition = 1;
//Create new dispatch for load data
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
// Load data in here
// Call main thread to update UI
dispatch_async(dispatch_get_main_queue(), ^{
baseView.hidden = YES;
});
});https://stackoverflow.com/questions/29885743
复制相似问题