Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 26 additions & 17 deletions DACircularProgress/DACircularProgressView.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

#import <QuartzCore/QuartzCore.h>

static NSString *const kIndeterminateAnimationKey = @"indeterminateAnimation";
static NSString *const kProgressAnimationKey = @"progress";

@interface DACircularProgressLayer : CALayer

@property(nonatomic, strong) UIColor *trackTintColor;
Expand Down Expand Up @@ -167,6 +170,10 @@ - (void)didMoveToWindow
CGFloat windowContentsScale = self.window.screen.scale;
self.circularProgressLayer.contentsScale = windowContentsScale;
[self.circularProgressLayer setNeedsDisplay];

if (_indeterminate) {
[self addIndeterminateAnimation];
}
}


Expand Down Expand Up @@ -203,8 +210,8 @@ - (void)setProgress:(CGFloat)progress
initialDelay:(CFTimeInterval)initialDelay
withDuration:(CFTimeInterval)duration
{
[self.layer removeAnimationForKey:@"indeterminateAnimation"];
[self.circularProgressLayer removeAnimationForKey:@"progress"];
[self.layer removeAnimationForKey:kIndeterminateAnimationKey];
[self.circularProgressLayer removeAnimationForKey:kProgressAnimationKey];

CGFloat pinnedProgress = MIN(MAX(progress, 0.0f), 1.0f);
if (animated) {
Expand All @@ -216,7 +223,7 @@ - (void)setProgress:(CGFloat)progress
animation.toValue = [NSNumber numberWithFloat:pinnedProgress];
animation.beginTime = CACurrentMediaTime() + initialDelay;
animation.delegate = self;
[self.circularProgressLayer addAnimation:animation forKey:@"progress"];
[self.circularProgressLayer addAnimation:animation forKey:kProgressAnimationKey];
} else {
[self.circularProgressLayer setNeedsDisplay];
self.circularProgressLayer.progress = pinnedProgress;
Expand Down Expand Up @@ -287,24 +294,26 @@ - (void)setThicknessRatio:(CGFloat)thicknessRatio
[self.circularProgressLayer setNeedsDisplay];
}

- (NSInteger)indeterminate
- (void)setIndeterminate:(NSInteger)indeterminate
{
CAAnimation *spinAnimation = [self.layer animationForKey:@"indeterminateAnimation"];
return (spinAnimation == nil ? 0 : 1);
_indeterminate = indeterminate;
if (_indeterminate) {
[self addIndeterminateAnimation];
} else {
[self.layer removeAnimationForKey:kIndeterminateAnimationKey];
}
}

- (void)setIndeterminate:(NSInteger)indeterminate
- (void)addIndeterminateAnimation
{
if (indeterminate) {
if (!self.indeterminate) {
CABasicAnimation *spinAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
spinAnimation.byValue = [NSNumber numberWithDouble:indeterminate > 0 ? 2.0f*M_PI : -2.0f*M_PI];
spinAnimation.duration = self.indeterminateDuration;
spinAnimation.repeatCount = HUGE_VALF;
[self.layer addAnimation:spinAnimation forKey:@"indeterminateAnimation"];
}
} else {
[self.layer removeAnimationForKey:@"indeterminateAnimation"];
CAAnimation *indeterminateAnimation = [self.layer animationForKey:kIndeterminateAnimationKey];

if (!indeterminateAnimation) {
CABasicAnimation *spinAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
spinAnimation.byValue = [NSNumber numberWithDouble:_indeterminate > 0 ? 2.0f*M_PI : -2.0f*M_PI];
spinAnimation.duration = self.indeterminateDuration;
spinAnimation.repeatCount = HUGE_VALF;
[self.layer addAnimation:spinAnimation forKey:kIndeterminateAnimationKey];
}
}

Expand Down