Created
October 21, 2012 15:52
-
-
Save kristopherjohnson/3927325 to your computer and use it in GitHub Desktop.
Display activity indicator over dimmed screen
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (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