Skip to content

Commit

Permalink
Implement indefinite spinner
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcus Kida committed Apr 5, 2016
1 parent ad39792 commit 167eabd
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
1 change: 1 addition & 0 deletions Pod/Classes/ios/NYTLoadingIndicatorView.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
57 changes: 57 additions & 0 deletions Pod/Classes/ios/NYTLoadingIndicatorView.m
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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];
}
Expand All @@ -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

0 comments on commit 167eabd

Please sign in to comment.