显示微调,并在同一个块中删除(show spinner and remove it in the s

2019-09-03 00:13发布

在一个方法可能需要长达几秒钟,我有:

UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(135,140,50,50)];
spinner.color = [UIColor blueColor];
[spinner startAnimating];
[_mapViewController.view addSubview:spinner];

// lots of code

[spinner removeFromSuperview];

该微调不出来。 大概因为屏幕不会在那个时候得到更新。 我怎样才能解决这个问题呢?

Answer 1:

使用GCD:

UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(135,140,50,50)];
spinner.color = [UIColor blueColor];
[spinner startAnimating];
[_mapViewController.view addSubview:spinner];

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    // lots of code run in the background

    dispatch_async(dispatch_get_main_queue(), ^{
        // stop and remove the spinner on the main thread when done
        [spinner removeFromSuperview];
    });
});


文章来源: show spinner and remove it in the same block