0

我想加载一个视图,但是加载需要很长时间,所以我尝试放置一个activityIndi​​catorView,但是当视图已经加载时它开始运行,当我返回主视图时它仍在运行并且它不会停止.

我该怎么做才能让activityIndi​​catorView在我点击UIButton时立即启动并在它完成加载时停止?

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

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

}
4

1 回答 1

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];
    });
}); 
于 2013-09-30T03:26:17.710 回答