diff --git a/DACircularProgress/DACircularProgressView.m b/DACircularProgress/DACircularProgressView.m index ec4e30f..4e75648 100644 --- a/DACircularProgress/DACircularProgressView.m +++ b/DACircularProgress/DACircularProgressView.m @@ -10,6 +10,9 @@ #import +static NSString *const kIndeterminateAnimationKey = @"indeterminateAnimation"; +static NSString *const kProgressAnimationKey = @"progress"; + @interface DACircularProgressLayer : CALayer @property(nonatomic, strong) UIColor *trackTintColor; @@ -167,6 +170,10 @@ - (void)didMoveToWindow CGFloat windowContentsScale = self.window.screen.scale; self.circularProgressLayer.contentsScale = windowContentsScale; [self.circularProgressLayer setNeedsDisplay]; + + if (_indeterminate) { + [self addIndeterminateAnimation]; + } } @@ -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) { @@ -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; @@ -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]; } }