Guest User

Untitled

a guest
Oct 16th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1.  
  2. #import "MPActivityIndicator.h"
  3.  
  4. #import <QuartzCore/QuartzCore.h>
  5.  
  6. #define WIDTH 320
  7. #define HEIGHT 480
  8.  
  9. #define kBGImageSmall [UIImage imageNamed:@"Loadingcircle.png"]
  10. #define kBGImageBig [UIImage imageNamed:@"Refreshcircle.png"]
  11.  
  12. #define ANIMATION_DURATION 1.0f
  13.  
  14. @interface MPActivityIndicator () {
  15. UIImageView *__background;
  16. UIImageView *__loader;
  17.  
  18. BOOL __animating;
  19.  
  20. CGFloat __previousRotationAngle;
  21. }
  22.  
  23. @end
  24.  
  25. @implementation MPActivityIndicator
  26.  
  27. @synthesize animating = __animating;
  28.  
  29. #pragma mark -
  30. #pragma mark Lifecycle
  31.  
  32.  
  33. - (id)init {
  34.  
  35. self = [super init];
  36.  
  37. if (self) {
  38. self.backgroundColor = [UIColor clearColor];
  39. // place init code here
  40.  
  41. __previousRotationAngle = 0.0;
  42.  
  43. __loader = [[UIImageView alloc] initWithImage:kLoaderImageBig];
  44. __background = [[UIImageView alloc] initWithImage:kBGImageBig];
  45.  
  46. [self addSubview:__background];
  47. [self addSubview:__loader];
  48. }
  49.  
  50. return self;
  51. }
  52.  
  53. - (void)layoutSubviews {
  54.  
  55. [super layoutSubviews];
  56.  
  57. if ( !CGRectEqualToRect(__loader.frame, self.bounds) ) {
  58. __loader.frame = self.bounds;
  59. }
  60.  
  61. if ( !CGRectEqualToRect(__background.frame, self.bounds) ) {
  62. __background.frame = self.bounds;
  63. }
  64.  
  65. }
  66.  
  67. #pragma mark -
  68. #pragma mark Public
  69.  
  70. - (void)rotateLoadingCircleByPercents:(CGFloat)percents {
  71.  
  72. if ( __animating ) {
  73. return;
  74. }
  75.  
  76. CGFloat ratio = fabsf(percents) / 100.0;
  77. CGFloat radians = ratio * M_PI * 2.0;
  78. CGFloat newradians = radians - __previousRotationAngle;
  79. __previousRotationAngle = radians;
  80.  
  81. CATransform3D matrix = __loader.layer.transform;
  82. matrix.m34 = 1.0 / -500.0;
  83. __loader.layer.transform = CATransform3DRotate(matrix, newradians, 0.0, 0.0, 1.0);
  84.  
  85. }
  86.  
  87. - (void)startAnimating {
  88.  
  89. if ( __animating ) {
  90. return;
  91. }
  92.  
  93. [__loader.layer removeAnimationForKey:@"rotationAnimation"];
  94.  
  95. CABasicAnimation* rotationAnimation;
  96. rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
  97. rotationAnimation.fromValue = [NSNumber numberWithFloat:0.0];
  98. rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 ];
  99. rotationAnimation.duration = ANIMATION_DURATION;
  100. rotationAnimation.additive = YES;
  101. rotationAnimation.repeatCount = HUGE_VALF;
  102.  
  103. [__loader.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
  104.  
  105. __animating = YES;
  106. }
  107.  
  108. - (void)stopAnimating {
  109.  
  110. [__loader.layer removeAnimationForKey:@"rotationAnimation"];
  111. __loader.layer.transform = CATransform3DIdentity;
  112.  
  113. __animating = NO;
  114. }
  115.  
  116. #pragma mark -
  117. #pragma mark Memory management
  118.  
  119. - (void)dealloc {
  120.  
  121. TW_RELEASE( __loader );
  122. TW_RELEASE( __background );
  123.  
  124. [super dealloc];
  125. }
  126.  
  127. @end
Add Comment
Please, Sign In to add comment