1

在我的 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];
}

我发现了一个有点相似的线程: UIActivityIndi​​catorView not shown until after loading done

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

我是初学者,如果我的问题根本上是错误的,我很抱歉。

4

3 回答 3

2

对不起,丑陋的文本格式。有一种非常可爱的方式可以做你想做的事。首先,您必须显示指标视图,并且仅在一小段时间(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];
    });
}
于 2014-02-24T06:29:51.517 回答
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 做同样的事情。当后台线程完成加载滚动视图时,我可以在主线程的队列中显示滚动视图。但前一种解决方案也可以正常工作......)

于 2012-08-31T10:21:30.870 回答
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];
于 2014-01-08T12:17:19.513 回答