How show activity-indicator when press button for upload next view or webview?

萝らか妹 提交于 2019-12-04 09:22:57

Toro's suggestion offers a great explanation and solution, but I just wanted to offer up another way of achieving this, as this is how I do it.

As Toro said,

- (void) someFunction
{
    [activityIndicator startAnimation];

    // do computations ....

    [activityIndicator stopAnimation];  
}

The above code will not work because you do not give the UI time to update when you include the activityIndicator in your currently running function. So what I and many others do is break it up into a separate thread like so:

- (void) yourMainFunction {
    activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];

    [NSThread detachNewThreadSelector:@selector(threadStartAnimating) toTarget:self withObject:nil];

    //Your computations

    [activityIndicator stopAnimating];

}

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

Good luck! -Karoly

[self.navigationController pushViewController:first animated:YES];

Generally, when you push a view controller into navigation controller, it will invoke the -(void)viewWillAppear: and -(void)viewDidAppear: methods. You can add activity indicator view inside the viewWillAppear: and call startAnimation of indicator view. You CANNOT invoke startAnimation and stopAnimation at the same time. For example,

- (void)viewWillAppear:(BOOL)animated 
{
    [aIndicatorView startAnimation];

    // do somethings ....

    [aIndicatorView stopAnimation];  
}

Because the startAnimation and stopAnimation are under the same time, then no animation will show.

But if you invoke startAnimation in -(void)viewWillAppear: and invoke stopAnimation in another message, like followings.

- (void)viewWillAppear:(BOOL)animated
{
    [aIndicatorView startAnimation];

    // do somethings... 
}

- (void)viewDidAppear:(BOOL)animated
{
    [aIndicatorView stopAnimation];
}

Because viewWillAppear: and viewDidAppear: are invoked with different event time, the activity indicator view will work well.

Or, you can do something like followings:

- (void)viewWillAppear:(BOOL)animated 
{
    [aIndicatorView startAnimation];

    // Let run loop has chances to animations, others events in run loop queue, and ... etc. 
    [[NSRunLoop currentRunLoop] runUntilDate:[NSDate date]];

    // do somethings ....

    [aIndicatorView stopAnimation];
}

The above example is a bad example, because it invokes two or more animations in the -runUntilDate:. But it will let the activity indicator view work.

Create a webview. Add a activity indicator to the webview. If you are loading a image via url into the webview then implement the webview delegate methods. Once the url is loaded then stopanimating the activity indicator.

Let me know which step you are not able to implement.

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