Why won't my UIActivityIndicatorView stop animating?

早过忘川 提交于 2020-01-02 07:06:32

问题


I am trying to manage the activity indicator from my App Delegate, that way any of my views can put the indicator up. So, I am adding it as a subview to 'window' and start/stop as follows:

- (void)didStartActivity
{
    if( activityIndicator == nil ) {
        activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
        activityIndicator.hidesWhenStopped = YES;
        activityIndicator.center = window.center;
        activityIndicator.transform = CGAffineTransformScale(CGAffineTransformIdentity, 6.0, 6.0);
    }
    NSLog(@"%s: starting the activityIndicator", __FUNCTION__);
    [window addSubview:activityIndicator];
    [activityIndicator startAnimating];
}

I see the log messages, so I know the code is being invoked. The indicator is at the center and 6x the default size. However, the stopAnimating isn't stopping. The only thing I can conclude is that it needs to run in the present view controller.

- (void)didStopActivity
{
    NSLog(@"%s: stopping the activityIndicator", __FUNCTION__);
    [activityIndicator stopAnimating];
    [activityIndicator removeFromSuperview];
}

回答1:


Are you trying to do this from a background thread? An easy and definitive test would be to add:

NSLog(@"thread: %@", [NSThread currentThread]);



回答2:


check if your activityIndicator isn't nil:

NSLog(@"activityIndicator: %@", activityIndicator);



回答3:


OK. I did my experiment and sure enough, it worked flawlessly when I added the activity indicator as a subview of viewcontroller's view. However, when I then used that new simpleton project to try it as a subview to window it also worked. Obviously a bug in my code and it needs more inspection.

I will award both Eimantas with the answer as the debug notion was helpful in my solution.




回答4:


if your log is nil then do this:

in ".h" declare :

__strong UIActivityIndicatorView *activityIndicator

the __strong avoid ARC and then you can remove and stop the activityIndicator.

[activityIndicator stopAnimating];
[activityIndicator removeFromSuperview];



回答5:


If you are adding it to the superview more than one time it will not work.

Try putting

[superView addSubview:activityIndicator];

into the

if( activityIndicator == nil ) {

condition



来源:https://stackoverflow.com/questions/2168902/why-wont-my-uiactivityindicatorview-stop-animating

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!