BennoDual
BennoDual

Reputation: 6259

show animation and locking the current view

I have a method which is calling a external webservice. Now I want to show an UIActivityIndicatorView in the middle of the screen, while the webservice-call is running. I call the webservice-method asynchronously. But with my code which you will find at the bottom, I have two problems:

  1. the current view is not locked. That means, that the user can interact with the current view. I want to block all till the animation is stopped
  2. the animation is not centered

Here is my code which I have tried now:

-(void)saveDataOnServer {
    UIActivityIndicatorView *activity = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(225, 115, 30, 30)];
    [activity setBackgroundColor:[UIColor clearColor]];
    [activity setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray];
    [self.view addSubview:activity];
    [activity startAnimating];

    // ...     

    [NSURLConnection sendAsynchronousRequest: request
                                       queue: queue
                           completionHandler: ^(NSURLResponse *response, NSData *data, NSError *error) {

                               [activity stopAnimating];

                               // ...
                           }
     ];
}

Upvotes: 0

Views: 390

Answers (2)

Ponting
Ponting

Reputation: 2246

You have to put another view when activity indicator start :

 UIView *overlayView;
 overlayView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
 overlayView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
 [[UIApplication sharedApplication].keyWindow addSubview:overlayView];

And remove that view(overlayView) when activity indicator stops :

 [overlayView removeFromSuperview];

This OverlayView will cover current view.So user can not interact with current view.

Upvotes: 2

iJazJaz
iJazJaz

Reputation: 45

Hi there i have a good solution for you .. there i a library called MBProgressHUD which does all you want , WITH Style This is The link for the library HERE

And its easy to use .. here is an example i used

self.hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
self.hud.mode = MBProgressHUDModeIndeterminate;
self.hud.labelText = @"Fetching News";
//self.hud.detailsLabelText=@"Fetching News";
self.hud.dimBackground = YES;

and there is a demo you can see .. Cheers :D

Upvotes: 0

Related Questions