正在计算内容时的iOS活动指示器

时间:2013-11-12 20:56:59

标签: ios uiactivityindicatorview

如果重复了主题,我很抱歉,但我找不到解决方案。 我正在尝试进行一些计算,在执行它们时,我希望用户在刷卡后看到活动指示器。用户应该看到指示符并等待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];
}

1 个答案:

答案 0 :(得分: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];}];
    }