iPhone UIActivityIndi​​catorView 未启动或停止

发布于 2024-08-13 10:55:29 字数 127 浏览 6 评论 0原文

当我在 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?]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

離人涙 2024-08-20 10:55:29

如果您编写这样的代码:

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

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

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

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

或者,您可以将计算放在单独的线程上,并在调用 stopAnimation 之前等待其完成。

If you write code like this:

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

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

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

樱桃奶球 2024-08-20 10:55:29

我通常这样做:

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

...

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

I usually do:

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

...

- (void)lotsOfComputation {
    ...
    [activityIndicator stopAnimating];
}
魂牵梦绕锁你心扉 2024-08-20 10:55:29

这个问题很有用。但答案中缺少的一件事是,每一件需要很长时间的事情都需要在单独的线程中执行,而不是在 UIActivityIndi​​catorView 中执行。这样它就不会停止响应 UI 界面。

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

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

This question is quite useful. But one thing that is missing in the answer post is , every thing that takes long time need to be perform in separate thread not the UIActivityIndicatorView. This way it won't stop responding to UI interface.

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

    -(void)doStuff{
    [activityIndicator startAnimating]; 
    [NSThread detachNewThreadSelector:@selector(doLotsOFWork:) toTarget:self withObject:nil]; 
    [activityIndicator stopAnimating];
}
北城半夏 2024-08-20 10:55:29

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

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

-(void)startIndicator{
    [activityIndicator startAnimating];
}

All UI elements require to be on main thread

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

then:

-(void)startIndicator{
    [activityIndicator startAnimating];
}
昔日梦未散 2024-08-20 10:55:29

如果需要,可以使用 swift 3 版本:

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

If needed, swift 3 version:

func doSomething() {
    activityIndicator.startAnimating()
    DispatchQueue.global(qos: .background).async {
        //do some processing intensive stuff
        DispatchQueue.main.async {
            self.activityIndicator.stopAnimating()
        }
    }
}
爱*していゐ 2024-08-20 10:55:29

好吧,抱歉,我的代码似乎是盲目的。

我已经这样结束了指示器:

    [activityIndicator removeFromSuperview];
activityIndicator = nil;

因此,在一次运行之后,activityIndi​​cator 已被完全删除。

Ok, sorry seems like I went through my code being blind.

I've ended the Indicator like this:

    [activityIndicator removeFromSuperview];
activityIndicator = nil;

So after one run, the activityIndicator has been removed completely.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文