Created
July 11, 2013 09:01
-
-
Save gimenete/5973821 to your computer and use it in GitHub Desktop.
Show an activity indicator in any UIView. Example: [imageView showActivityIndicator]; Then hide it [imageView hideActivityIndicator];
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
#import <Foundation/Foundation.h> | |
@interface UIView (ActivityIndicator) | |
- (void)showActivityIndicator; | |
- (void)showActivityIndicatorWithStyle:(UIActivityIndicatorViewStyle)style; | |
- (void)hideActivityIndicator; | |
@end |
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
#import "UIView+ActivityIndicator.h" | |
#import <QuartzCore/QuartzCore.h> | |
@implementation UIView (ActivityIndicator) | |
- (void)showActivityIndicator { | |
[self showActivityIndicatorWithStyle:UIActivityIndicatorViewStyleWhiteLarge]; | |
} | |
- (void)showActivityIndicatorWithStyle:(UIActivityIndicatorViewStyle)style { | |
CGRect frame = self.frame; | |
frame.origin.x = 0; | |
frame.origin.y = 0; | |
UIView *view = [[UIView alloc] initWithFrame:frame]; | |
view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; | |
view.backgroundColor = [UIColor darkGrayColor]; | |
view.layer.opacity = 0.5; | |
view.tag = 1001; | |
[self addSubview:view]; | |
UIActivityIndicatorView* indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:style]; | |
indicator.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | |
| UIViewAutoresizingFlexibleRightMargin | |
| UIViewAutoresizingFlexibleTopMargin | |
| UIViewAutoresizingFlexibleBottomMargin; | |
indicator.tag = 1002; | |
indicator.center = view.center; | |
[indicator startAnimating]; | |
[self addSubview:indicator]; | |
} | |
- (void)hideActivityIndicator { | |
[[self viewWithTag:1001] removeFromSuperview]; | |
[[self viewWithTag:1002] removeFromSuperview]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment