Skip to content

Instantly share code, notes, and snippets.

@martinpilch
Created May 31, 2012 18:33
Show Gist options
  • Save martinpilch/2845295 to your computer and use it in GitHub Desktop.
Save martinpilch/2845295 to your computer and use it in GitHub Desktop.
Custom activity indicator
@interface MPActivityIndicator : UIView
@property (nonatomic, readonly, getter = isAnimating) BOOL animating;
- (void)stopAnimating;
- (void)startAnimating;
- (void)rotateLoadingCircleByPercents:(CGFloat)percents;
@end
#import "MPActivityIndicator.h"
#import <QuartzCore/QuartzCore.h>
#define WIDTH 320
#define HEIGHT 480
#define kBGImageSmall [UIImage imageNamed:@"Loadingcircle.png"]
#define kBGImageBig [UIImage imageNamed:@"Refreshcircle.png"]
#define ANIMATION_DURATION 1.0f
@interface MPActivityIndicator () {
UIImageView *__background;
UIImageView *__loader;
BOOL __animating;
CGFloat __previousRotationAngle;
}
@end
@implementation MPActivityIndicator
@synthesize animating = __animating;
#pragma mark -
#pragma mark Lifecycle
- (id)init {
self = [super init];
if (self) {
self.backgroundColor = [UIColor clearColor];
// place init code here
__previousRotationAngle = 0.0;
__loader = [[UIImageView alloc] initWithImage:kLoaderImageBig];
__background = [[UIImageView alloc] initWithImage:kBGImageBig];
[self addSubview:__background];
[self addSubview:__loader];
}
return self;
}
- (void)layoutSubviews {
[super layoutSubviews];
if ( !CGRectEqualToRect(__loader.frame, self.bounds) ) {
__loader.frame = self.bounds;
}
if ( !CGRectEqualToRect(__background.frame, self.bounds) ) {
__background.frame = self.bounds;
}
}
#pragma mark -
#pragma mark Public
- (void)rotateLoadingCircleByPercents:(CGFloat)percents {
if ( __animating ) {
return;
}
CGFloat ratio = fabsf(percents) / 100.0;
CGFloat radians = ratio * M_PI * 2.0;
CGFloat newradians = radians - __previousRotationAngle;
__previousRotationAngle = radians;
CATransform3D matrix = __loader.layer.transform;
matrix.m34 = 1.0 / -500.0;
__loader.layer.transform = CATransform3DRotate(matrix, newradians, 0.0, 0.0, 1.0);
}
- (void)startAnimating {
if ( __animating ) {
return;
}
[__loader.layer removeAnimationForKey:@"rotationAnimation"];
CABasicAnimation* rotationAnimation;
rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
rotationAnimation.fromValue = [NSNumber numberWithFloat:0.0];
rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 ];
rotationAnimation.duration = ANIMATION_DURATION;
rotationAnimation.additive = YES;
rotationAnimation.repeatCount = HUGE_VALF;
[__loader.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
__animating = YES;
}
- (void)stopAnimating {
[__loader.layer removeAnimationForKey:@"rotationAnimation"];
__loader.layer.transform = CATransform3DIdentity;
__animating = NO;
}
#pragma mark -
#pragma mark Memory management
- (void)dealloc {
TW_RELEASE( __loader );
TW_RELEASE( __background );
[super dealloc];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment