iPhone UIActivityIndi​​catorView无法启动或停止 [英] iPhone UIActivityIndicatorView not starting or stopping

查看:72
本文介绍了iPhone UIActivityIndi​​catorView无法启动或停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在UIActivityIndi​​catorView上调用startAnimating时,它无法启动。这是为什么?

When I call startAnimating on a UIActivityIndicatorView, it doesn't start. Why is this?

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

[This is a blog-style self-answered question. The solution below works for me, but, maybe there are others that are better?]

推荐答案

如果您编写如下代码:

If you write code like this:

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

您没有给UI时间实际启动和停止活动指示器,因为你的所有计算都在主线程上。一种解决方案是在单独的线程中调用startAnimating:

You aren't giving the UI time to actually start and stop the activity indicator, because all of your computation is on the main thread. One solution is to call startAnimating in a separate thread:

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

- (void)doStuff
{ 
    [NSThread detachNewThreadSelector:@selector(threadStartAnimating:) toTarget:self withObject:nil];
    ...lots of computation...
    [activityIndicator stopAnimating];
}

或者,你可以将计算放在一个单独的线程上,等待它在调用stopAnimation之前完成。

Or, you could put your computation on a separate thread, and wait for it to finish before calling stopAnimation.

这篇关于iPhone UIActivityIndi​​catorView无法启动或停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆