Skip to content

Commit 4ac4289

Browse files
committed
Restore spin animation if indeterminate is YES when view is added to a window
Fixes danielamitay#61.
1 parent 0bae503 commit 4ac4289

File tree

1 file changed

+26
-17
lines changed

1 file changed

+26
-17
lines changed

DACircularProgress/DACircularProgressView.m

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010

1111
#import <QuartzCore/QuartzCore.h>
1212

13+
static NSString *const kIndeterminateAnimationKey = @"indeterminateAnimation";
14+
static NSString *const kProgressAnimationKey = @"progress";
15+
1316
@interface DACircularProgressLayer : CALayer
1417

1518
@property(nonatomic, strong) UIColor *trackTintColor;
@@ -167,6 +170,10 @@ - (void)didMoveToWindow
167170
CGFloat windowContentsScale = self.window.screen.scale;
168171
self.circularProgressLayer.contentsScale = windowContentsScale;
169172
[self.circularProgressLayer setNeedsDisplay];
173+
174+
if (_indeterminate) {
175+
[self addIndeterminateAnimation];
176+
}
170177
}
171178

172179

@@ -203,8 +210,8 @@ - (void)setProgress:(CGFloat)progress
203210
initialDelay:(CFTimeInterval)initialDelay
204211
withDuration:(CFTimeInterval)duration
205212
{
206-
[self.layer removeAnimationForKey:@"indeterminateAnimation"];
207-
[self.circularProgressLayer removeAnimationForKey:@"progress"];
213+
[self.layer removeAnimationForKey:kIndeterminateAnimationKey];
214+
[self.circularProgressLayer removeAnimationForKey:kProgressAnimationKey];
208215

209216
CGFloat pinnedProgress = MIN(MAX(progress, 0.0f), 1.0f);
210217
if (animated) {
@@ -216,7 +223,7 @@ - (void)setProgress:(CGFloat)progress
216223
animation.toValue = [NSNumber numberWithFloat:pinnedProgress];
217224
animation.beginTime = CACurrentMediaTime() + initialDelay;
218225
animation.delegate = self;
219-
[self.circularProgressLayer addAnimation:animation forKey:@"progress"];
226+
[self.circularProgressLayer addAnimation:animation forKey:kProgressAnimationKey];
220227
} else {
221228
[self.circularProgressLayer setNeedsDisplay];
222229
self.circularProgressLayer.progress = pinnedProgress;
@@ -287,24 +294,26 @@ - (void)setThicknessRatio:(CGFloat)thicknessRatio
287294
[self.circularProgressLayer setNeedsDisplay];
288295
}
289296

290-
- (NSInteger)indeterminate
297+
- (void)setIndeterminate:(NSInteger)indeterminate
291298
{
292-
CAAnimation *spinAnimation = [self.layer animationForKey:@"indeterminateAnimation"];
293-
return (spinAnimation == nil ? 0 : 1);
299+
_indeterminate = indeterminate;
300+
if (_indeterminate) {
301+
[self addIndeterminateAnimation];
302+
} else {
303+
[self.layer removeAnimationForKey:kIndeterminateAnimationKey];
304+
}
294305
}
295306

296-
- (void)setIndeterminate:(NSInteger)indeterminate
307+
- (void)addIndeterminateAnimation
297308
{
298-
if (indeterminate) {
299-
if (!self.indeterminate) {
300-
CABasicAnimation *spinAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
301-
spinAnimation.byValue = [NSNumber numberWithDouble:indeterminate > 0 ? 2.0f*M_PI : -2.0f*M_PI];
302-
spinAnimation.duration = self.indeterminateDuration;
303-
spinAnimation.repeatCount = HUGE_VALF;
304-
[self.layer addAnimation:spinAnimation forKey:@"indeterminateAnimation"];
305-
}
306-
} else {
307-
[self.layer removeAnimationForKey:@"indeterminateAnimation"];
309+
CAAnimation *indeterminateAnimation = [self.layer animationForKey:kIndeterminateAnimationKey];
310+
311+
if (!indeterminateAnimation) {
312+
CABasicAnimation *spinAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
313+
spinAnimation.byValue = [NSNumber numberWithDouble:_indeterminate > 0 ? 2.0f*M_PI : -2.0f*M_PI];
314+
spinAnimation.duration = self.indeterminateDuration;
315+
spinAnimation.repeatCount = HUGE_VALF;
316+
[self.layer addAnimation:spinAnimation forKey:kIndeterminateAnimationKey];
308317
}
309318
}
310319

0 commit comments

Comments
 (0)