From 167eabd5053495568e2a8f3392276d94243e7d94 Mon Sep 17 00:00:00 2001 From: Marcus Kida Date: Wed, 6 Apr 2016 09:06:31 +1000 Subject: [PATCH] Implement indefinite spinner --- Pod/Classes/ios/NYTLoadingIndicatorView.h | 1 + Pod/Classes/ios/NYTLoadingIndicatorView.m | 57 +++++++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/Pod/Classes/ios/NYTLoadingIndicatorView.h b/Pod/Classes/ios/NYTLoadingIndicatorView.h index 6fd1d37b..3db319d2 100644 --- a/Pod/Classes/ios/NYTLoadingIndicatorView.h +++ b/Pod/Classes/ios/NYTLoadingIndicatorView.h @@ -14,5 +14,6 @@ * The progress to be shown, ranging 0.0 to 1.0 */ @property (nonatomic, assign) CGFloat progress; +@property (nonatomic, assign, getter=isIndefinite) BOOL indefinite; @end diff --git a/Pod/Classes/ios/NYTLoadingIndicatorView.m b/Pod/Classes/ios/NYTLoadingIndicatorView.m index 3407c61c..6e5b9d06 100644 --- a/Pod/Classes/ios/NYTLoadingIndicatorView.m +++ b/Pod/Classes/ios/NYTLoadingIndicatorView.m @@ -7,10 +7,20 @@ // #import "NYTLoadingIndicatorView.h" +#define kIndefiniteLoaderWidth 10 + +typedef NS_ENUM(NSInteger, NYTLoadingAnimationDirection) { + NYTLoadingAnimationDirectionRight, + NYTLoadingAnimationDirectionLeft +}; @interface NYTLoadingIndicatorView () @property (nonatomic) UIView *indicatorView; +@property (nonatomic) NSTimer *animationTimer; + +@property (nonatomic) NYTLoadingAnimationDirection animationDirection; +@property (nonatomic) CGFloat animationPosition; @end @@ -50,6 +60,12 @@ - (void)resetIndicatorHidden:(BOOL)hidden { } - (void)updateIndicatorView { + // indefinite spinner animation + if (self.isIndefinite) { + return; + } + + // definite spinner animation if (self.progress == 0.0f) { return [self resetIndicatorHidden:NO]; } @@ -63,9 +79,50 @@ - (CGFloat)progressWidth:(CGFloat)progress { return (self.bounds.size.width / 100) * (progress * 100); } +- (void)indefiniteAnimation:(BOOL)start { + self.indicatorView.hidden = !start; + if (start) { + self.indicatorView.frame = (CGRect){0, 0, kIndefiniteLoaderWidth, self.bounds.size.height}; + self.animationTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/60 + target:self + selector:@selector(indefiniteAnimation) + userInfo:nil + repeats:YES]; + return; + } + if (self.animationTimer.isValid) { + [self.animationTimer invalidate]; + } +} + +- (void)indefiniteAnimation { + if (self.indicatorView.frame.origin.x >= self.bounds.size.width + kIndefiniteLoaderWidth) { + self.animationDirection = NYTLoadingAnimationDirectionLeft; + } else if (self.indicatorView.frame.origin.x <= 0 - kIndefiniteLoaderWidth) { + self.animationDirection = NYTLoadingAnimationDirectionRight; + } + + if (self.animationDirection == NYTLoadingAnimationDirectionRight) { + self.animationPosition += self.bounds.size.width / (kIndefiniteLoaderWidth * 10); + } else { + self.animationPosition -= self.bounds.size.width/ (kIndefiniteLoaderWidth * 10); + } + self.indicatorView.frame = (CGRect){self.animationPosition, 0, kIndefiniteLoaderWidth, self.bounds.size.height}; +} + +#pragma mark - Accessors - (void)setProgress:(CGFloat)progress { _progress = progress; + [self setIndefinite:NO]; [self updateIndicatorView]; } +- (void)setIndefinite:(BOOL)indefinite { + if (_indefinite == indefinite) { + return; + } + [self indefiniteAnimation:indefinite]; + _indefinite = indefinite; +} + @end