-
Notifications
You must be signed in to change notification settings - Fork 527
/
M13ProgressViewPie.m
510 lines (439 loc) · 18.5 KB
/
M13ProgressViewPie.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
//
// M13ProgressViewPie.m
// M13ProgressView
//
/*Copyright (c) 2013 Brandon McQuilkin
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#import "M13ProgressViewPie.h"
@interface M13ProgressViewPie ()
/**The start progress for the progress animation.*/
@property (nonatomic, assign) CGFloat animationFromValue;
/**The end progress for the progress animation.*/
@property (nonatomic, assign) CGFloat animationToValue;
/**The start time interval for the animaiton.*/
@property (nonatomic, assign) CFTimeInterval animationStartTime;
/**Link to the display to keep animations in sync.*/
@property (nonatomic, strong) CADisplayLink *displayLink;
/**Allow us to write to the progress.*/
@property (nonatomic, readwrite) CGFloat progress;
/**The layer that progress is shown on.*/
@property (nonatomic, retain) CAShapeLayer *progressLayer;
/**The layer that the background and indeterminate progress is shown on.*/
@property (nonatomic, retain) CAShapeLayer *backgroundLayer;
/**The layer that is used to render icons for success or failure.*/
@property (nonatomic, retain) CAShapeLayer *iconLayer;
/**The layer that is used to display the indeterminate view.*/
@property (nonatomic, retain) CAShapeLayer *indeterminateLayer;
/**The action currently being performed.*/
@property (nonatomic, assign) M13ProgressViewAction currentAction;
@end
#define kM13ProgressViewPieHideKey @"Hide"
#define kM13ProgressViewPieShowKey @"Show"
@implementation M13ProgressViewPie
{
//Wether or not the corresponding values have been overriden by the user
BOOL _backgroundRingWidthOverriden;
}
@dynamic progress;
#pragma mark Initalization and setup
- (id)init
{
self = [super init];
if (self) {
[self setup];
}
return self;
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setup];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
[self setup];
}
return self;
}
- (void)setup
{
//Set own background color
self.backgroundColor = [UIColor clearColor];
//Set defaut sizes
_backgroundRingWidthOverriden = NO;
_backgroundRingWidth = fmaxf(self.bounds.size.width * .025, 1.0);
self.animationDuration = .3;
//Set default colors
self.primaryColor = [UIColor colorWithRed:0 green:122/255.0 blue:1.0 alpha:1.0];
self.secondaryColor = self.primaryColor;
//Set up the background layer
_backgroundLayer = [CAShapeLayer layer];
_backgroundLayer.strokeColor = self.secondaryColor.CGColor;
_backgroundLayer.fillColor = [UIColor clearColor].CGColor;
_backgroundLayer.lineCap = kCALineCapRound;
_backgroundLayer.lineWidth = _backgroundRingWidth;
[self.layer addSublayer:_backgroundLayer];
//Set up the progress layer
_progressLayer = [CAShapeLayer layer];
_progressLayer.fillColor = self.primaryColor.CGColor;
_progressLayer.backgroundColor = [UIColor clearColor].CGColor;
[self.layer addSublayer:_progressLayer];
//Set the indeterminate layer
_indeterminateLayer = [CAShapeLayer layer];
_indeterminateLayer.fillColor = self.primaryColor.CGColor;
_indeterminateLayer.opacity = 0;
[self.layer addSublayer:_indeterminateLayer];
//Set up the icon layer
_iconLayer = [CAShapeLayer layer];
_iconLayer.fillColor = self.primaryColor.CGColor;
_iconLayer.fillRule = kCAFillRuleNonZero;
[self.layer addSublayer:_iconLayer];
}
#pragma mark Appearance
- (void)setPrimaryColor:(UIColor *)primaryColor
{
[super setPrimaryColor:primaryColor];
_progressLayer.strokeColor = self.primaryColor.CGColor;
_progressLayer.fillColor = self.primaryColor.CGColor;
_iconLayer.fillColor = self.primaryColor.CGColor;
_indeterminateLayer.fillColor = self.primaryColor.CGColor;
[self setNeedsDisplay];
}
- (void)setSecondaryColor:(UIColor *)secondaryColor
{
[super setSecondaryColor:secondaryColor];
_backgroundLayer.strokeColor = self.secondaryColor.CGColor;
[self setNeedsDisplay];
}
- (void)setBackgroundRingWidth:(CGFloat)backgroundRingWidth
{
_backgroundRingWidth = backgroundRingWidth;
_backgroundLayer.lineWidth = _backgroundRingWidth;
_backgroundRingWidthOverriden = YES;
[self setNeedsDisplay];
[self invalidateIntrinsicContentSize];
}
#pragma mark Actions
- (void)setProgress:(CGFloat)progress animated:(BOOL)animated
{
if (self.progress == progress) {
return;
}
if (animated == NO) {
if (_displayLink) {
//Kill running animations
[_displayLink removeFromRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
_displayLink = nil;
}
[super setProgress:progress animated:animated];
[self setNeedsDisplay];
} else {
_animationStartTime = CACurrentMediaTime();
_animationFromValue = self.progress;
_animationToValue = progress;
if (!_displayLink) {
//Create and setup the display link
[self.displayLink removeFromRunLoop:NSRunLoop.mainRunLoop forMode:NSRunLoopCommonModes];
self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(animateProgress:)];
[self.displayLink addToRunLoop:NSRunLoop.mainRunLoop forMode:NSRunLoopCommonModes];
} /*else {
//Reuse the current display link
}*/
}
}
- (void)animateProgress:(CADisplayLink *)displayLink
{
dispatch_async(dispatch_get_main_queue(), ^{
CGFloat dt = (displayLink.timestamp - _animationStartTime) / self.animationDuration;
if (dt >= 1.0) {
//Order is important! Otherwise concurrency will cause errors, because setProgress: will detect an animation in progress and try to stop it by itself. Once over one, set to actual progress amount. Animation is over.
[self.displayLink removeFromRunLoop:NSRunLoop.mainRunLoop forMode:NSRunLoopCommonModes];
self.displayLink = nil;
[super setProgress:_animationToValue animated:NO];
[self setNeedsDisplay];
return;
}
//Set progress
[super setProgress:_animationFromValue + dt * (_animationToValue - _animationFromValue) animated:YES];
[self setNeedsDisplay];
});
}
- (void)performAction:(M13ProgressViewAction)action animated:(BOOL)animated
{
if (action == M13ProgressViewActionNone && _currentAction != M13ProgressViewActionNone) {
//Animate
[CATransaction begin];
[_iconLayer addAnimation:[self hideAnimation] forKey:kM13ProgressViewPieHideKey];
if (self.indeterminate) {
[_indeterminateLayer addAnimation:[self showAnimation] forKey:kM13ProgressViewPieShowKey];
} else {
[_progressLayer addAnimation:[self showAnimation] forKey:kM13ProgressViewPieShowKey];
}
[CATransaction commit];
_currentAction = action;
} else if (action == M13ProgressViewActionSuccess && _currentAction != M13ProgressViewActionSuccess) {
if (_currentAction == M13ProgressViewActionNone) {
_currentAction = action;
//Just show the icon layer
[self drawIcon];
//Animate
[CATransaction begin];
[_iconLayer addAnimation:[self showAnimation] forKey:kM13ProgressViewPieShowKey];
if (self.indeterminate) {
[_indeterminateLayer addAnimation:[self hideAnimation] forKey:kM13ProgressViewPieHideKey];
} else {
[_progressLayer addAnimation:[self hideAnimation] forKey:kM13ProgressViewPieHideKey];
}
[CATransaction commit];
} else if (_currentAction == M13ProgressViewActionFailure) {
//Hide the icon layer before showing
[CATransaction begin];
[_iconLayer addAnimation:[self hideAnimation] forKey:kM13ProgressViewPieHideKey];
[CATransaction setCompletionBlock:^{
_currentAction = action;
[self drawIcon];
[_iconLayer addAnimation:[self showAnimation] forKey:kM13ProgressViewPieShowKey];
}];
[CATransaction commit];
}
} else if (action == M13ProgressViewActionFailure && _currentAction != M13ProgressViewActionFailure) {
if (_currentAction == M13ProgressViewActionNone) {
//Just show the icon layer
_currentAction = action;
[self drawIcon];
[CATransaction begin];
[_iconLayer addAnimation:[self showAnimation] forKey:kM13ProgressViewPieShowKey];
if (self.indeterminate) {
[_indeterminateLayer addAnimation:[self hideAnimation] forKey:kM13ProgressViewPieHideKey];
} else {
[_progressLayer addAnimation:[self hideAnimation] forKey:kM13ProgressViewPieHideKey];
}
[CATransaction commit];
} else if (_currentAction == M13ProgressViewActionSuccess) {
//Hide the icon layer before showing
[CATransaction begin];
[_iconLayer addAnimation:[self hideAnimation] forKey:kM13ProgressViewPieHideKey];
[CATransaction setCompletionBlock:^{
_currentAction = action;
[self drawIcon];
[_iconLayer addAnimation:[self showAnimation] forKey:kM13ProgressViewPieShowKey];
}];
[CATransaction commit];
}
}
}
- (void)setIndeterminate:(BOOL)indeterminate
{
[super setIndeterminate:indeterminate];
if (self.indeterminate == YES) {
//Draw the indeterminate circle
[self drawIndeterminate];
//Create the rotation animation
CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0];
rotationAnimation.duration = 1;
rotationAnimation.cumulative = YES;
rotationAnimation.repeatCount = HUGE_VALF;
//Set the animations
[_indeterminateLayer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
[CATransaction begin];
[_progressLayer addAnimation:[self hideAnimation] forKey:kM13ProgressViewPieHideKey];
[_indeterminateLayer addAnimation:[self showAnimation] forKey:kM13ProgressViewPieShowKey];
[CATransaction commit];
} else {
//Animate
[CATransaction begin];
[_progressLayer addAnimation:[self showAnimation] forKey:kM13ProgressViewPieShowKey];
[_indeterminateLayer addAnimation:[self hideAnimation] forKey:kM13ProgressViewPieHideKey];
[CATransaction setCompletionBlock:^{
//Remove the rotation animation and reset the background
[_backgroundLayer removeAnimationForKey:@"rotationAnimation"];
[self drawBackground];
}];
[CATransaction commit];
}
}
- (CABasicAnimation *)showAnimation
{
//Show the progress layer and percentage
CABasicAnimation *showAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
showAnimation.fromValue = [NSNumber numberWithFloat:0.0];
showAnimation.toValue = [NSNumber numberWithFloat:1.0];
showAnimation.duration = self.animationDuration;
showAnimation.repeatCount = 1.0;
//Prevent the animation from resetting
showAnimation.fillMode = kCAFillModeForwards;
showAnimation.removedOnCompletion = NO;
return showAnimation;
}
- (CABasicAnimation *)hideAnimation
{
//Hide the progress layer and percentage
CABasicAnimation *hideAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
hideAnimation.fromValue = [NSNumber numberWithFloat:1.0];
hideAnimation.toValue = [NSNumber numberWithFloat:0.0];
hideAnimation.duration = self.animationDuration;
hideAnimation.repeatCount = 1.0;
//Prevent the animation from resetting
hideAnimation.fillMode = kCAFillModeForwards;
hideAnimation.removedOnCompletion = NO;
return hideAnimation;
}
#pragma mark Layout
- (void)layoutSubviews
{
//Update frames of layers
_backgroundLayer.frame = self.bounds;
_progressLayer.frame = self.bounds;
_iconLayer.frame = self.bounds;
_indeterminateLayer.frame = self.bounds;
//Update line widths if not overriden
if (!_backgroundRingWidthOverriden) {
_backgroundRingWidth = fmaxf(self.frame.size.width * .025, 1.0);
}
//Redraw
[self setNeedsDisplay];
}
- (CGSize)intrinsicContentSize
{
if (!_backgroundRingWidthOverriden) {
//Based on scale
return CGSizeMake(UIViewNoIntrinsicMetric, UIViewNoIntrinsicMetric);
} else {
return CGSizeMake(2 * _backgroundRingWidth, 2 * _backgroundRingWidth);
}
}
- (void)setFrame:(CGRect)frame
{
//Keep the progress view square.
if (frame.size.width != frame.size.height) {
frame.size.height = frame.size.width;
}
[super setFrame:frame];
}
#pragma mark Drawing
- (void)drawRect:(CGRect)rect
{
[super drawRect:rect];
//Draw the background
[self drawBackground];
//Draw Icons
[self drawIcon];
//Draw Progress
[self drawProgress];
}
- (void)drawSuccess
{
//Draw relative to a base size and percentage, that way the check can be drawn for any size.*/
CGFloat radius = (self.frame.size.width / 2.0);
CGFloat size = radius * .3;
//Create the path
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(0, 0)];
[path addLineToPoint:CGPointMake(0, size * 2)];
[path addLineToPoint:CGPointMake(size * 3, size * 2)];
[path addLineToPoint:CGPointMake(size * 3, size)];
[path addLineToPoint:CGPointMake(size, size)];
[path addLineToPoint:CGPointMake(size, 0)];
[path closePath];
//Rotate it through -45 degrees...
[path applyTransform:CGAffineTransformMakeRotation(-M_PI_4)];
//Center it
[path applyTransform:CGAffineTransformMakeTranslation(radius * .46, 1.02 * radius)];
//Set path
[_iconLayer setPath:path.CGPath];
[_iconLayer setFillColor:self.primaryColor.CGColor];
}
- (void)drawFailure
{
//Calculate the size of the X
CGFloat radius = self.frame.size.width / 2.0;
CGFloat size = radius * .3;
//Create the path for the X
UIBezierPath *xPath = [UIBezierPath bezierPath];
[xPath moveToPoint:CGPointMake(size, 0)];
[xPath addLineToPoint:CGPointMake(2 * size, 0)];
[xPath addLineToPoint:CGPointMake(2 * size, size)];
[xPath addLineToPoint:CGPointMake(3 * size, size)];
[xPath addLineToPoint:CGPointMake(3 * size, 2 * size)];
[xPath addLineToPoint:CGPointMake(2 * size, 2 * size)];
[xPath addLineToPoint:CGPointMake(2 * size, 3 * size)];
[xPath addLineToPoint:CGPointMake(size, 3 * size)];
[xPath addLineToPoint:CGPointMake(size, 2 * size)];
[xPath addLineToPoint:CGPointMake(0, 2 * size)];
[xPath addLineToPoint:CGPointMake(0, size)];
[xPath addLineToPoint:CGPointMake(size, size)];
[xPath closePath];
//Center it
[xPath applyTransform:CGAffineTransformMakeTranslation(radius - (1.5 * size), radius - (1.5 * size))];
//Rotate path
[xPath applyTransform:CGAffineTransformMake(cos(M_PI_4),sin(M_PI_4),-sin(M_PI_4),cos(M_PI_4),radius * (1 - cos(M_PI_4)+ sin(M_PI_4)),radius * (1 - sin(M_PI_4)- cos(M_PI_4)))];
//Set path and fill color
[_iconLayer setPath:xPath.CGPath];
[_iconLayer setFillColor:self.primaryColor.CGColor];
}
- (void)drawBackground
{
//Create parameters to draw background
float startAngle = - M_PI_2;
float endAngle = startAngle + (2.0 * M_PI);
CGPoint center = CGPointMake(self.bounds.size.width / 2.0, self.bounds.size.width / 2.0);
CGFloat radius = (self.bounds.size.width - _backgroundRingWidth) / 2.0;
//Draw path
UIBezierPath *path = [UIBezierPath bezierPath];
path.lineWidth = _backgroundRingWidth;
path.lineCapStyle = kCGLineCapRound;
[path addArcWithCenter:center radius:radius startAngle:startAngle endAngle:endAngle clockwise:YES];
//Set the path
_backgroundLayer.path = path.CGPath;
}
- (void)drawProgress
{
//Create parameters to draw progress
float startAngle = - M_PI_2;
float endAngle = startAngle + (2.0 * M_PI * self.progress);
CGPoint center = CGPointMake(self.bounds.size.width / 2.0, self.bounds.size.width / 2.0);
CGFloat radius = (self.bounds.size.width - _backgroundRingWidth) / 2.0;
//Draw path
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(self.bounds.size.width / 2.0, self.bounds.size.height / 2.0)];
[path addArcWithCenter:center radius:radius startAngle:startAngle endAngle:endAngle clockwise:YES];
[path closePath];
//Set the path
[_progressLayer setPath:path.CGPath];
}
- (void)drawIcon
{
if (_currentAction == M13ProgressViewActionSuccess) {
[self drawSuccess];
} else if (_currentAction == M13ProgressViewActionFailure) {
[self drawFailure];
} else if (_currentAction == M13ProgressViewActionNone) {
//Clear layer
_iconLayer.path = nil;
}
}
- (void)drawIndeterminate
{
//Create parameters to draw progress
float startAngle = - M_PI_2;
float endAngle = startAngle + (2.0 * M_PI * .2);
CGPoint center = CGPointMake(self.bounds.size.width / 2.0, self.bounds.size.width / 2.0);
CGFloat radius = (self.bounds.size.width - _backgroundRingWidth) / 2.0;
//Draw path
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(self.bounds.size.width / 2.0, self.bounds.size.height / 2.0)];
[path addArcWithCenter:center radius:radius startAngle:startAngle endAngle:endAngle clockwise:YES];
[path closePath];
//Set the path
[_indeterminateLayer setPath:path.CGPath];
}
@end