Customized Application Loader

Introduction

A loader is something in a service based app for loading data from the server, it takes time to download heavy data such as audio, images and so on. For a better user experience developers show an activity view until the download completes.

The following is the procedure to create and use it in an application.

Step 1

Open XCode by double-clicking on it.

Step 2

Create a UIView class like the following.

UIView Class

Step 3

Take User Interface View like the following:

user interface view

Step 4

Now here we will write the code in the UIView class Objective-C file.

BActivityIndicator.h

  1. #import < UIKit / UIKit.h >  
  2.   
  3. @interface BActivityIndicator: UIView {  
  4.     CGAffineTransform rotationTransform;  
  5. }@property(weak, nonatomic) IBOutlet UILabel * pleaseWaitLabel;@property(weak, nonatomic) IBOutlet UIImageView * spinningImage;  
  6.   
  7. - (id) initWithView: (UIView * ) view;  
  8.   
  9. + (BActivityIndicator * ) showHUDAddedTo: (UIView * ) view animated: (BOOL) animated;  
  10.   
  11. + (BOOL) hideHUDForView: (UIView * ) view animated: (BOOL) animated;  
  12.   
  13. @end  
BActivityIndicator.m
  1. #import "BActivityIndicator.h"  
  2.   
  3. @implementation BActivityIndicator  
  4.   
  5. - (id) initWithFrame: (CGRect) frame {  
  6.     self = [super initWithFrame: frame];  
  7.     if (self) {  
  8.         self = [  
  9.             [  
  10.                 [NSBundle mainBundle] loadNibNamed: @  
  11.                 "BActivityIndicator"  
  12.                 owner: self  
  13.                 options: nil] lastObject];  
  14.         self.frame = frame;  
  15.         [self rotateLayerInfinite];  
  16.   
  17.         self.pleaseWaitLabel.font = [UIFont fontWithName: @  
  18.         "RobotoCondensed-Regular"  
  19.         size: 14.0f];  
  20.     }  
  21.     return self;  
  22. }  
  23.   
  24. - (id) initWithView: (UIView * ) view {  
  25.     // Let's check if the view is nil (this is a common error when using the windw initializer above)  
  26.     if (!view) {  
  27.         [NSException raise: @  
  28.         "MBProgressHUDViewIsNillException"  
  29.         format: @  
  30.         "The view used in the MBProgressHUD initializer is nil."];  
  31.     }  
  32.     id me = [self initWithFrame: CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];  
  33.     // We need to take care of rotation ourselfs if we're adding the HUD to a window  
  34.     if ([view isKindOfClass: [UIWindow class]]) {  
  35.         [self setTransformForCurrentOrientation: NO];  
  36.     }  
  37.     [  
  38.         [NSNotificationCenter defaultCenter] addObserver: self selector: @selector(deviceOrientationDidChange: )  
  39.         name: UIDeviceOrientationDidChangeNotification object: nil];  
  40.   
  41.     return me;  
  42. }  
  43.   
  44.   
  45. - (void) rotateLayerInfinite {  
  46.     CABasicAnimation * rotation;  
  47.     rotation = [CABasicAnimation animationWithKeyPath: @  
  48.     "transform.rotation"];  
  49.     rotation.fromValue = [NSNumber numberWithFloat: 0];  
  50.     rotation.toValue = [NSNumber numberWithFloat: (2 * M_PI)];  
  51.     rotation.duration = 1.5f;  
  52.     rotation.repeatCount = HUGE_VALF;  
  53.     [_spinningImage.layer removeAllAnimations];  
  54.     [_spinningImage.layer addAnimation: rotation forKey: @  
  55.     "Spin"];  
  56. }  
  57.  
  58. #pragma mark - #pragma mark Manual oritentation change  
  59.  
  60. #define RADIANS(degrees)((degrees * (float) M_PI) / 180.0f)  
  61.   
  62. - (void) deviceOrientationDidChange: (NSNotification * ) notification {  
  63.     if (!self.superview) {  
  64.         return;  
  65.     }  
  66.     if ([self.superview isKindOfClass: [UIWindow class]]) {  
  67.         [self setTransformForCurrentOrientation: YES];  
  68.     }  
  69. }  
  70.   
  71. - (void) setTransformForCurrentOrientation: (BOOL) animated {  
  72.     UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;  
  73.     NSInteger degrees = 0;  
  74.   
  75.     // Stay in sync with the superview  
  76.     if (self.superview) {  
  77.         self.bounds = self.superview.bounds;  
  78.         [self setNeedsDisplay];  
  79.     }  
  80.   
  81.     if (UIInterfaceOrientationIsLandscape(orientation)) {  
  82.         if (orientation == UIInterfaceOrientationLandscapeLeft) {  
  83.             degrees = -90;  
  84.         } else {  
  85.             degrees = 90;  
  86.         }  
  87.         // Window coordinates differ!  
  88.         self.bounds = CGRectMake(0, 0, self.bounds.size.height, self.bounds.size.width);  
  89.     } else {  
  90.         if (orientation == UIInterfaceOrientationPortraitUpsideDown) {  
  91.             degrees = 180;  
  92.         } else {  
  93.             degrees = 0;  
  94.         }  
  95.     }  
  96.   
  97.     rotationTransform = CGAffineTransformMakeRotation(RADIANS(degrees));  
  98.   
  99.     if (animated) {  
  100.         [UIView beginAnimations: nil context: nil];  
  101.     }  
  102.     [self setTransform: rotationTransform];  
  103.     if (animated) {  
  104.         [UIView commitAnimations];  
  105.     }  
  106. }  
  107.   
  108. + (BActivityIndicator * ) showHUDAddedTo: (UIView * ) view animated: (BOOL) animated {  
  109.     BActivityIndicator * hud = [  
  110.         [BActivityIndicator alloc] initWithView: view];  
  111.     [view addSubview: hud];  
  112.     return hud;  
  113. }  
  114.   
  115. + (BOOL) hideHUDForView: (UIView * ) view animated: (BOOL) animated {  
  116.     UIView * viewToRemove = nil;  
  117.     for (UIView * v in [view subviews]) {  
  118.         if ([v isKindOfClass: [BActivityIndicator class]]) {  
  119.             viewToRemove = v;  
  120.         }  
  121.     }  
  122.     if (viewToRemove != nil) {  
  123.         [viewToRemove removeFromSuperview];  
  124.         return YES;  
  125.     } else {  
  126.         return NO;  
  127.     }  
  128. }  
  129.   
  130. - (void) dealloc {  
  131.     [_spinningImage.layer removeAnimationForKey: @  
  132.     "Spin"];  
  133. }  
  134.   
  135. @end  
ViewController.Xib

ViewController

Step 7

Now here I’ll explain how to call that class within a project.

In your common Utility Class you need to write these functions for calling it:
  1. #pragma mark - Activity Indicator + (void) startActivityIndicatorInView: (UIView * ) aView withMessage: (NSString * ) aMessage {  
  2.     //    MBProgressHUD *_hud = [MBProgressHUD showHUDAddedTo:aView animated:YES];  
  3.     //    _hud.dimBackground  = YES;  
  4.     //    _hud.labelText      = aMessage;  
  5.   
  6.     BActivityIndicator * _hud = [BActivityIndicator showHUDAddedTo: aView animated: YES];  
  7.     //    _hud.pleaseWaitLabel.text  = aMessage;  
  8.   
  9. }  
  10.   
  11. + (void) stopActivityIndicatorInView: (UIView * ) aView {  
  12.     // [MBProgressHUD hideHUDForView:aView animated:YES];  
  13.     [BActivityIndicator hideHUDForView: aView animated: YES];  
  14. }  
Output

output

 

Up Next
    Ebook Download
    View all
    Learn
    View all