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

output