Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kristopherjohnson/3927325 to your computer and use it in GitHub Desktop.
Save kristopherjohnson/3927325 to your computer and use it in GitHub Desktop.
Display activity indicator over dimmed screen
- (IBAction)dimScreenAndShowActivityIndicator:(id)sender {
CGRect bounds = self.view.bounds;
// Create black view covering entire view
UIView *maskView = [[UIView alloc] initWithFrame:bounds];
maskView.backgroundColor = [UIColor blackColor];
[self.view addSubview:maskView];
// Put spinning activity indicator in middle of view
UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
activityIndicator.frame = bounds;
[maskView addSubview:activityIndicator];
[activityIndicator startAnimating];
// Put a label below the activity indicator
CGRect statusLabelFrame = CGRectMake(0, 0, 320, 20);
UILabel *statusLabel = [[UILabel alloc] initWithFrame:statusLabelFrame];
statusLabel.center = CGPointMake(maskView.center.x, maskView.center.y + 32);
statusLabel.textColor = [UIColor whiteColor];
statusLabel.backgroundColor = [UIColor clearColor];
statusLabel.text = @"Submitting Report";
statusLabel.textAlignment = NSTextAlignmentCenter;
statusLabel.font = [UIFont boldSystemFontOfSize:14];
[maskView addSubview:statusLabel];
// Fade in the screen-covering view
maskView.alpha = 0.0;
[UIView animateWithDuration:0.6 animations:^{
[maskView setAlpha:0.8];
}];
// After delay, fade the view out and remove it
int64_t delayInSeconds = 2.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[activityIndicator stopAnimating];
statusLabel.text = @"";
[UIView animateWithDuration:0.6
animations:^{
[maskView setAlpha:0];
} completion:^(BOOL finished) {
[maskView removeFromSuperview];
}];
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment