10
10
11
11
#import < QuartzCore/QuartzCore.h>
12
12
13
+ static NSString *const kIndeterminateAnimationKey = @" indeterminateAnimation" ;
14
+ static NSString *const kProgressAnimationKey = @" progress" ;
15
+
13
16
@interface DACircularProgressLayer : CALayer
14
17
15
18
@property (nonatomic , strong ) UIColor *trackTintColor;
@@ -167,6 +170,10 @@ - (void)didMoveToWindow
167
170
CGFloat windowContentsScale = self.window .screen .scale ;
168
171
self.circularProgressLayer .contentsScale = windowContentsScale;
169
172
[self .circularProgressLayer setNeedsDisplay ];
173
+
174
+ if (_indeterminate) {
175
+ [self addIndeterminateAnimation ];
176
+ }
170
177
}
171
178
172
179
@@ -203,8 +210,8 @@ - (void)setProgress:(CGFloat)progress
203
210
initialDelay : (CFTimeInterval )initialDelay
204
211
withDuration : (CFTimeInterval )duration
205
212
{
206
- [self .layer removeAnimationForKey: @" indeterminateAnimation " ];
207
- [self .circularProgressLayer removeAnimationForKey: @" progress " ];
213
+ [self .layer removeAnimationForKey: kIndeterminateAnimationKey ];
214
+ [self .circularProgressLayer removeAnimationForKey: kProgressAnimationKey ];
208
215
209
216
CGFloat pinnedProgress = MIN (MAX (progress, 0 .0f ), 1 .0f );
210
217
if (animated) {
@@ -216,7 +223,7 @@ - (void)setProgress:(CGFloat)progress
216
223
animation.toValue = [NSNumber numberWithFloat: pinnedProgress];
217
224
animation.beginTime = CACurrentMediaTime () + initialDelay;
218
225
animation.delegate = self;
219
- [self .circularProgressLayer addAnimation: animation forKey: @" progress " ];
226
+ [self .circularProgressLayer addAnimation: animation forKey: kProgressAnimationKey ];
220
227
} else {
221
228
[self .circularProgressLayer setNeedsDisplay ];
222
229
self.circularProgressLayer .progress = pinnedProgress;
@@ -287,24 +294,26 @@ - (void)setThicknessRatio:(CGFloat)thicknessRatio
287
294
[self .circularProgressLayer setNeedsDisplay ];
288
295
}
289
296
290
- - (NSInteger )indeterminate
297
+ - (void ) setIndeterminate : ( NSInteger )indeterminate
291
298
{
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
+ }
294
305
}
295
306
296
- - (void )setIndeterminate : ( NSInteger ) indeterminate
307
+ - (void )addIndeterminateAnimation
297
308
{
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 ];
308
317
}
309
318
}
310
319
0 commit comments