Skip to content

Instantly share code, notes, and snippets.

@PauloMigAlmeida
Created February 12, 2014 00:03
Show Gist options
  • Save PauloMigAlmeida/8947125 to your computer and use it in GitHub Desktop.
Save PauloMigAlmeida/8947125 to your computer and use it in GitHub Desktop.
UIView Category which hides every component inside and shows up an activityIndicator while you do things in background
//
// UIView+LoadingWithActivityIndicator.h
//
// Created by Paulo Miguel Almeida on 2/11/14.
// Copyright (c) 2014 Paulo Miguel Almeida. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface UIView (LoadingWithActivityIndicator)
-(void) startLoadingAnimation;
-(void) stopLoadingAnimation;
@end
//
// UIView+LoadingWithActivityIndicator.m
//
// Created by Paulo Miguel Almeida on 2/11/14.
// Copyright (c) 2014 Paulo Miguel Almeida. All rights reserved.
//
#import "UIView+LoadingWithActivityIndicator.h"
@implementation UIView (LoadingWithActivityIndicator)
-(void) startLoadingAnimation
{
UIActivityIndicatorView* activityIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[activityIndicator setFrame:CGRectMake((self.frame.size.width - activityIndicator.frame.size.width) / 2 , (self.frame.size.height - activityIndicator.frame.size.height) / 2 , activityIndicator.frame.size.width , activityIndicator.frame.size.width)];
for(UIView* view in self.subviews)
{
[view setHidden:YES];
}
[self addSubview:activityIndicator];
[activityIndicator startAnimating];
}
-(void) stopLoadingAnimation
{
for(UIView* view in self.subviews)
{
if([view isKindOfClass:[UIActivityIndicatorView class]])
{
UIActivityIndicatorView* activityIndicator = (UIActivityIndicatorView*)view;
[activityIndicator stopAnimating];
[activityIndicator removeFromSuperview];
}
else
{
[view setHidden:NO];
}
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment