Skip to content

Instantly share code, notes, and snippets.

@stephsharp
Created July 28, 2013 23:40
Show Gist options
  • Save stephsharp/6100742 to your computer and use it in GitHub Desktop.
Save stephsharp/6100742 to your computer and use it in GitHub Desktop.

Revisions

  1. stephsharp created this gist Jul 28, 2013.
    15 changes: 15 additions & 0 deletions LoadingView.h
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    //
    // 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
    79 changes: 79 additions & 0 deletions LoadingView.m
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,79 @@
    //
    // 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