Created
July 28, 2013 23:40
-
-
Save stephsharp/6100742 to your computer and use it in GitHub Desktop.
Simple iOS loading view
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
// | |
// LoadingView.h | |
// Created by Stephanie Sharp on 26/05/13. | |
// | |
// http://www.sitepoint.com/all-purpose-loading-view-for-ios/ | |
// http://www.cocoawithlove.com/2009/04/showing-message-over-iphone-keyboard.html | |
#import <UIKit/UIKit.h> | |
@interface LoadingView : UIView | |
+(LoadingView *)loadingViewInView:(UIView *)superView; | |
-(void)removeView; | |
@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
// | |
// LoadingView.m | |
// Created by Stephanie Sharp on 26/05/13. | |
// | |
#import "LoadingView.h" | |
#import <QuartzCore/QuartzCore.h> | |
@implementation LoadingView | |
- (id)initWithFrame:(CGRect)frame | |
{ | |
self = [super initWithFrame:frame]; | |
if (self) { | |
// Initialization code | |
} | |
return self; | |
} | |
#pragma mark - Class methods | |
+(LoadingView *)loadingViewInView:(UIView *)superView | |
{ | |
// Create a new view with the same frame size as the superView | |
LoadingView * loadingView = [[LoadingView alloc] initWithFrame:superView.bounds]; | |
// If something's gone wrong, abort! | |
if(!loadingView) | |
return nil; | |
loadingView.opaque = NO; | |
// Create background view and add as subview | |
UIView * background = [[UIView alloc] initWithFrame:loadingView.frame]; | |
loadingView.backgroundColor = [UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:0.5f]; | |
[loadingView addSubview:background]; | |
// Add activity indicator | |
UIActivityIndicatorView * indicator = [[UIActivityIndicatorView alloc] | |
initWithActivityIndicatorStyle: UIActivityIndicatorViewStyleWhiteLarge]; | |
// Set the resizing mask so it's not stretched | |
indicator.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | | |
UIViewAutoresizingFlexibleRightMargin | | |
UIViewAutoresizingFlexibleBottomMargin | | |
UIViewAutoresizingFlexibleLeftMargin; | |
// Place it in the middle of the view | |
indicator.center = superView.center; | |
// Add it into the loadingView | |
[loadingView addSubview:indicator]; | |
// Start it animating | |
[indicator startAnimating]; | |
// Add the loading view to the superView. | |
[superView addSubview:loadingView]; | |
// Create a new fade animation & add to superview | |
CATransition * animation = [CATransition animation]; | |
[animation setType:kCATransitionFade]; | |
[[superView layer] addAnimation:animation forKey:@"layerAnimation"]; | |
return loadingView; | |
} | |
#pragma mark | |
-(void)removeView | |
{ | |
CATransition * animation = [CATransition animation]; | |
[animation setType:kCATransitionFade]; | |
[[[self superview] layer] addAnimation:animation forKey:@"layerAnimation"]; | |
[super removeFromSuperview]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment