显示微调并将其删除在同一个块中

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

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

微调不显示。 可能是因为当时屏幕没有更新。 我怎样才能解决这个问题?

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