2013-05-13 6 views
7

でそれを削除します。はスピナーを示し、私は数秒かかることがあります方法では、同じブロック

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

スピナーが表示されません。おそらく、その時点で画面が更新されないためです。 この問題を回避するにはどうすればよいですか?

+3

別のスレッドでcode'の '//たくさん入れて。 –

+0

あなたはこのコードをどこに置いているのか、さらにいくつかのコードを表示できますか? –

答えて

22

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

ありがとう、私はすぐに試みます – clankill3r

+0

2番目の 'dispatch_async'では、これはメインスレッドで実行されていますか?私はアプリの残りの部分を実行する方法を呼び出すことができます – qaisjp

+0

これはまさに私が必要なものです! –

関連する問題