显示UIActivityIndi​​catorView直到加载UIView

时间:2012-08-27 15:07:52

标签: ios uiview uiactivityindicatorview

在我的UIViewController的loadView方法中,我启动了一个UIActivityIndi​​cator。稍后在loadView中加载一个复杂的UIView,加载大约需要2-3秒。 (更具体地说,UIScrollView有很多图片)。我的问题是UIActivityIndi​​cator微调器只有在我的其他层加载后才可见。 (这对我来说当然没用)。处理这个问题的正确方法是什么?

- (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];
}

我找到了一个类似的线程: UIActivityIndicatorView not showing until after loading done

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

我是初学者,如果我的问题根本不对,那就很抱歉。

3 个答案:

答案 0 :(得分:2)

我设法让它成为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做同样的事情。当后台线程完成加载滚动视图时,我可以在主线程的队列中显示scrollview。但前一个解决方案在某种程度上也可以工作......)

答案 1 :(得分:2)

抱歉丑陋的文字格式化。 有一种非常可爱的方式来做你想要的。 首先,你必须显示指标视图,并且只在经过一段时间(0.1秒甚至更小)后才要求你的scrollView填充。

- (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];
    });
}

答案 2 :(得分:0)

试试这个简单的方法,它对我有用....

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];