iPhone UIActivityIndicator视图未启动或停止

iPhone UIActivityIndicator视图未启动或停止,iphone,uiactivityindicatorview,Iphone,Uiactivityindicatorview,当我在UIActivityIndicator视图上调用StartImating时,它不会启动。为什么会这样 [这是一个博客式的自我回答问题。下面的解决方案对我很有效,但是,也许还有其他更好的解决方案?]如果您编写这样的代码: - (void) doStuff { [activityIndicator startAnimating]; ...lots of computation... [activityIndicator stopAnimating]; } [a

当我在UIActivityIndicator视图上调用StartImating时,它不会启动。为什么会这样


[这是一个博客式的自我回答问题。下面的解决方案对我很有效,但是,也许还有其他更好的解决方案?]

如果您编写这样的代码:

- (void) doStuff
{
    [activityIndicator startAnimating];
    ...lots of computation...
    [activityIndicator stopAnimating];
}
    [activityIndicator removeFromSuperview];
activityIndicator = nil;
您没有给UI实际启动和停止活动指示器的时间,因为您的所有计算都在主线程上。一种解决方案是在单独的线程中调用startAnimating:

- (void) threadStartAnimating:(id)data {
    [activityIndicator startAnimating];
}

- (void)doStuff
{ 
    [NSThread detachNewThreadSelector:@selector(threadStartAnimating:) toTarget:self withObject:nil];
    ...lots of computation...
    [activityIndicator stopAnimating];
}
或者,您可以将计算放在一个单独的线程上,等待它完成后再调用stopAnimation。

我通常这样做:

[activityIndicator startAnimating];
[self performSelector:@selector(lotsOfComputation) withObject:nil afterDelay:0.01];

...

- (void)lotsOfComputation {
    ...
    [activityIndicator stopAnimating];
}

好吧,对不起,我好像是瞎了眼才通过密码的

我已经结束了这样的指标:

- (void) doStuff
{
    [activityIndicator startAnimating];
    ...lots of computation...
    [activityIndicator stopAnimating];
}
    [activityIndicator removeFromSuperview];
activityIndicator = nil;

因此,运行一次后,activityIndicator已完全删除。

这个问题非常有用。但答题帖中缺少的一点是,每一件耗时较长的事情都需要在单独的线程中执行,而不是在UIActivityIndicatorView中执行。这样它就不会停止对UI界面的响应

    - (void) doLotsOFWork:(id)data {
    //  do the work here.
}

    -(void)doStuff{
    [activityIndicator startAnimating]; 
    [NSThread detachNewThreadSelector:@selector(doLotsOFWork:) toTarget:self withObject:nil]; 
    [activityIndicator stopAnimating];
}

所有UI元素都需要位于主线程上

[self performSelectorOnMainThread:@selector(startIndicator) withObject:nil waitUntilDone:NO];
然后:


如果需要,swift 3版本:

func doSomething() {
    activityIndicator.startAnimating()
    DispatchQueue.global(qos: .background).async {
        //do some processing intensive stuff
        DispatchQueue.main.async {
            self.activityIndicator.stopAnimating()
        }
    }
}

您可能想清楚地表明您正在发布一个博客式的自答问题。我也在这样做,不同的是我使用了-(void)performBlock:(void(^)(void))block afterDelay:(NSTimeInterval)delay;从我指定0.0开始,不显示进度指示器,0.01是100Hz监视器闪烁之间的时间。解决方案为Thx!有同样的问题。。(+1)此方法是否启动新线程。。???如果是,那么在何处以及如何停止它..?这将导致奇怪的错误(或崩溃),因为必须在主线程上调用
startAnimating
。在另一个线程上进行计算的第二个选择是正确的方法。我会选择这个。这是最好的解释。不过,有一个修复方法是,我将移动调用,以停止在已分离的方法中设置指示器的动画。一旦该方法完成,它将停止动画。