iPhone UIActivityIndicatorView not starting or stopping

前端 未结 6 2297
囚心锁ツ
囚心锁ツ 2020-12-17 23:32

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

6条回答
  •  执笔经年
    2020-12-18 00:09

    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.

提交回复
热议问题