在加载UIView之前显示UIActivityIndicatorView



在UIViewController的loadView方法中,我启动一个UIActivityIndicator。稍后在loadView中,我加载了一个复杂的UIView,大约需要2-3秒才能加载。(更具体地说,请查看大量图片)。我的问题是UIActivityIndicator微调器只有在我的另一个层也加载之后才可见。(这对我来说当然没用)。处理这个问题的正确方法是什么?

- (void)loadView {
    CGRect fullScreenRect=[[UIScreen mainScreen] bounds];
    UIView *contentView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 340, fullScreenRect.size.width)]autorelease];
    self.view = contentView;
    spinner = [[[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite] autorelease];
    spinner.frame = CGRectMake(0.0, 0.0, 40.0, 40.0);
    spinner.center = self.view.center;
    [self.view addSubview:spinner];
    [spinner startAnimating];

     scrollView=[[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 340, fullScreenRect.size.width)]autorelease];
     ...setting up scrollView...
     [self.view addSubview:scrollView];
}
- (void)viewDidLoad
{ 
    [super viewDidLoad];
    [spinner removeFromSuperview];
}

我发现了一条有点相似的线索:UIActivityIndicator加载完成后才显示的视图

但它建议在后台线程中加载东西,但据我所知,只有在主线程中才能加载和显示UIView。

我是一个初学者,如果我的问题根本错了,我很抱歉。

我按照Carl Veazey的建议成功了。事实上,这很容易。我生成了一个新的后台线程,并在那里加载了我的UIScrollView。

我在LoadView:中使用过

spinner = [[[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite] autorelease];
spinner.frame = CGRectMake(0.0, 0.0, 40.0, 40.0);
    spinner.center = self.view.center;
[self.view addSubview:spinner];
[spinner startAnimating];
[self performSelectorInBackground:@selector(populateScrollView) withObject:nil];

和:

-(void) populateScrollView{
    ...creating scrollView...
    [self.view addSubview:scrollView];
    [spinner removeFromSuperview];
}

它工作得很好。对我来说真正奇怪的是,我在后台线程中而不是在主线程中向UI添加滚动视图。我以为我只能在主线程中处理UI。

(我可以用GCD做同样的事情。当后台线程加载完滚动视图时,我可以在主线程的队列中显示滚动视图。但前一个解决方案在某种程度上也能工作…)

很抱歉使用了难看的文本格式。有一种非常可爱的方式可以做你想做的事。首先,你必须显示指示器视图,只有在一小段时间(0.1秒甚至更短)后,你才能要求滚动视图填充。

- (void)loadView {
    CGRect fullScreenRect=[[UIScreen mainScreen] bounds];
    UIView *contentView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 340, fullScreenRect.size.width)]autorelease];
    self.view = contentView;
    spinner = [[[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite] autorelease];
    spinner.frame = CGRectMake(0.0, 0.0, 40.0, 40.0);
    spinner.center = self.view.center;
    [self.view addSubview:spinner];
    [spinner startAnimating];
    double delayInSeconds = 0.1;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        scrollView=[[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 340, fullScreenRect.size.width)]autorelease];
     ...setting up scrollView...
       [self.view addSubview:scrollView];
    });
}

试试这个简单的方法,它对我来说效果很好…

UIActivityIndicatorView *activityIndicator= [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
activityIndicator.layer.cornerRadius = 05;
activityIndicator.opaque = NO;
activityIndicator.backgroundColor = [UIColor colorWithWhite:0.0f alpha:0.6f];
activityIndicator.center = self.view.center;
activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
[activityIndicator setColor:[UIColor colorWithRed:0.6 green:0.8 blue:1.0 alpha:1.0]];
[self.view addSubview: activityIndicator];

加载视图时添加以下行

//-- Add this line while processing to load your view
    [activityIndicator startAnimating];
//-- Add this line when after you view loaded
    [activityIndicator stopAnimating];

最新更新