Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #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
Add Comment
Please, Sign In to add comment