自定义uiactivityIndi​​catorView需要时间,同时加载View

时间:2013-09-30 02:49:08

标签: ios multithreading view grand-central-dispatch

我想加载一个视图,但是需要永远加载所以我尝试放置一个activityIndi​​catorView,但是当视图已经加载时它开始运行,当我回到主视图时它仍在运行并赢了停止。

我需要做什么才能在我点击UIButton时立即启动activityIndi​​catorView并在完成加载后停止?

我已经尝试了所有类似的问题,她在堆栈溢出但没有帮助。

- (IBAction)boletin:(id)sender {

    UIImage *statusImage = [UIImage imageNamed:@"1.png"];
    activityImageView = [[UIImageView alloc] initWithImage:statusImage];


    //Add more images which will be used for the animation
    activityImageView.animationImages = [NSArray arrayWithObjects:
                                     [UIImage imageNamed:@"1.png"],
                                     [UIImage imageNamed:@"2.png"],
                                     [UIImage imageNamed:@"3.png"],
                                     [UIImage imageNamed:@"4.png"],
                                     [UIImage imageNamed:@"5.png"],
                                     [UIImage imageNamed:@"6.png"],
                                     [UIImage imageNamed:@"7.png"],
                                     [UIImage imageNamed:@"8.png"],
                                     [UIImage imageNamed:@"9.png"],
                                     [UIImage imageNamed:@"10.png"],
                                     [UIImage imageNamed:@"11.png"],
                                     [UIImage imageNamed:@"12.png"],
                                     [UIImage imageNamed:@"13.png"],
                                     [UIImage imageNamed:@"14.png"],
                                     [UIImage imageNamed:@"15.png"],
                                     [UIImage imageNamed:@"16.png"],
                                     [UIImage imageNamed:@"17.png"],
                                     [UIImage imageNamed:@"18.png"],
                                     nil];


    //Set the duration of the animation (play with it
    //until it looks nice for you)
    activityImageView.animationDuration = 0.7;


    //Position the activity image view somewhere in
    //the middle of your current view
    activityImageView.frame = CGRectMake(self.view.frame.size.width/2 - 25, self.view.frame.size.height/2 - 75, 45, 45);

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

        [activityImageView startAnimating];
        [self.view addSubview:activityImageView];

        dispatch_async(dispatch_get_main_queue(), ^{
            [activityImageView stopAnimating];
        });
    }); 

}

1 个答案:

答案 0 :(得分:0)

您正在图像视图上调用startAnimating,并将其添加到视图层次结构中,离开主线程,这将会产生问题。从你的代码中不清楚究竟是什么阻塞并要求所有这些,但这样的事情对你有用:

// rest of your code is above here...
activityImageView.frame = CGRectMake(self.view.frame.size.width/2 - 25, self.view.frame.size.height/2 - 75, 45, 45);
[self.view addSubview:activityImageView];
[activityImageView startAnimating];

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    // Do whatever is taking a long time here...
    dispatch_async(dispatch_get_main_queue(), ^{
        [activityImageView stopAnimating];
        [activityImageView removeFromSuperview];
    });
});