0

トピックが重複していたら申し訳ありませんが、解決策が見つかりませんでした。私はいくつかの計算をしようとしていますが、それらを実行している間、スワイプした直後にユーザーにアクティビティ インジケーターを表示させたいと考えています。ユーザーには、インジケーターが表示され、textView と結果が表示されるのを待つ必要があります。私が書いたものを使用すると、インジケーターが表示されません(表示されて同時に消えると思います)。計算が開始される前にそれを表示し、計算の最後に非表示にする方法は?

-(IBAction)swipeRight:(UISwipeGestureRecognizer *) recognizer {
activityIndicator.hidden = NO;
[activityIndicator startAnimating];

//some calculations are being done here
textViewInfo.text = [NSString stringWithFormat:@"results of long calculations..."];
textViewInfo.alpha = 1;

activityIndicator.hidden = YES;
[activityIndicator stopAnimating];
}
4

1 に答える 1

1

アクティビティ インジケーターが同時に表示され、同時に表示されなくなるという仮定は正しいです。すべての UI 変更はメイン スレッドのキューに入れられ、UI を表示するときに実行されます。その結果、startAnimating と stopAnimating が次々に発生し、アクティビティ インジケーターが「非表示」になります。これに対抗するには、次の手順を実行します (構文を確認してください):-

-(IBAction)swipeRight:(UISwipeGestureRecognizer *) recognizer {
    // queue is a NSOperationQueue and is a property of the class
    [queue addOperation: [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(heavyWork) object:nil]];
    [activityIndicator startAnimating];
  }  
    -(void)heavyWork
    {
      //heavy work regarding textView
      [[NSOperationQueue mainQueue] addOperationWithBlock:^{
         activityIndicator.hidden=YES;
      [activityIndicator stopAnimating];}];
    }
于 2013-11-12T21:15:33.147 に答える