How show activity-indicator when press button for

2019-04-12 16:30发布

问题:

when i click on button which title is click here to enlarge then i want show activity indicator on the first view and remove when load this view.

but i go back then it show activity indicator which is shown in this view.

in first vie .m file i have use this code for action.

-(IBAction)btnSelected:(id)sender{
UIButton *button = (UIButton *)sender;
int whichButton = button.tag;
NSLog(@"Current TAG: %i", whichButton);
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
[spinner setCenter:CGPointMake(160,124)]; 
[self.view addSubview:spinner]; 
[spinner startAnimating];

if(whichButton==1)
{
    [spinner stopAnimating];

    first=[[FirstImage alloc]init];
    [self.navigationController pushViewController:first animated:YES];
    [spinner hidesWhenStopped ];

        }} 

in above code i have button action in which i call next view. Now i want show/display activity indicator when view upload. In next view i have a image view in which a image i upload i have declare an activity indicator which also not working. How do that?

回答1:

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



回答2:

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.



回答3:

[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 under same run loop. For example,

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

    // do somethings ....

    [aIndicatorView stopAnimation];  
}

Because the startAnimation and stopAnimation are under the same run loop, 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 run loop, the activity indicator view will work well.

Or, you can do something like followings:

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

    [[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.