iPhone UIActivityIndicatorView not starting or stopping

前端 未结 6 2228
囚心锁ツ
囚心锁ツ 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-17 23:55

    All UI elements require to be on main thread

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

    then:

    -(void)startIndicator{
        [activityIndicator startAnimating];
    }
    
    0 讨论(0)
  • 2020-12-17 23:57

    I usually do:

    [activityIndicator startAnimating];
    [self performSelector:@selector(lotsOfComputation) withObject:nil afterDelay:0.01];
    
    ...
    
    - (void)lotsOfComputation {
        ...
        [activityIndicator stopAnimating];
    }
    
    0 讨论(0)
  • 2020-12-18 00:01

    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.

    0 讨论(0)
  • 2020-12-18 00:05

    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];
    }
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-18 00:15

    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()
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题