-
Notifications
You must be signed in to change notification settings - Fork 527
/
M13ProgressViewMetro.m
378 lines (327 loc) · 11.5 KB
/
M13ProgressViewMetro.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
//
// M13ProgressViewMetro.m
// M13ProgressSuite
//
// Created by Brandon McQuilkin on 3/8/14.
// Copyright (c) 2014 Brandon McQuilkin. All rights reserved.
//
#import "M13ProgressViewMetro.h"
#import "M13ProgressViewMetroDotPolygon.h"
@interface M13ProgressViewMetro ()
/**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;
@end
@implementation M13ProgressViewMetroDot
{
M13ProgressViewAction currentAction;
}
#pragma mark Initalization
- (id)init
{
self = [super init];
if (self) {
[self setup];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
[self setup];
}
return self;
}
- (id)initWithLayer:(id)layer
{
self = [super initWithLayer:layer];
if (self) {
[self setup];
}
return self;
}
+ (id)layer
{
return [[M13ProgressViewMetroDot alloc] init];
}
- (void)setup
{
_highlighted = NO;
currentAction = M13ProgressViewActionNone;
_primaryColor = [UIColor colorWithRed:0 green:122/255.0 blue:1.0 alpha:1.0];
_secondaryColor = [UIColor colorWithRed:181/255.0 green:182/255.0 blue:183/255.0 alpha:1.0];
_successColor = [UIColor colorWithRed:63.0f/255.0f green:226.0f/255.0f blue:80.0f/255.0f alpha:1];
_failureColor = [UIColor colorWithRed:249.0f/255.0f green:37.0f/255.0f blue:0 alpha:1];
}
#pragma mark Setters
- (void)setHighlighted:(BOOL)highlighted
{
_highlighted = highlighted;
[self setNeedsDisplay];
}
- (void)setSuccessColor:(UIColor *)successColor
{
_successColor = successColor;
[self setNeedsDisplay];
}
- (void)setFailureColor:(UIColor *)failureColor
{
_failureColor = failureColor;
[self setNeedsDisplay];
}
- (void)setPrimaryColor:(UIColor *)primaryColor
{
_primaryColor = primaryColor;
[self setNeedsDisplay];
}
- (void)setSecondaryColor:(UIColor *)secondaryColor
{
_secondaryColor = secondaryColor;
[self setNeedsDisplay];
}
- (void)performAction:(M13ProgressViewAction)action animated:(BOOL)animated
{
currentAction = action;
[self setNeedsDisplay];
}
@end
@implementation M13ProgressViewMetro
{
BOOL animating;
NSUInteger currentNumberOfDots;
NSTimer *animationTimer;
UIBezierPath *animationPath;
NSMutableArray *dots;
CGFloat circleSize;
}
- (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 defaults
self.clipsToBounds = YES;
animating = NO;
_numberOfDots = 6;
_dotSize = CGSizeMake(20, 20);
self.animationDuration = 1.5;
self.animationShape = M13ProgressViewMetroAnimationShapeEllipse;
self.primaryColor = [UIColor colorWithRed:0 green:122/255.0 blue:1.0 alpha:1.0];
self.secondaryColor = [UIColor colorWithRed:181/255.0 green:182/255.0 blue:183/255.0 alpha:1.0];
_successColor = [UIColor colorWithRed:63.0f/255.0f green:226.0f/255.0f blue:80.0f/255.0f alpha:1];
_failureColor = [UIColor colorWithRed:249.0f/255.0f green:37.0f/255.0f blue:0 alpha:1];
_metroDot = [[M13ProgressViewMetroDotPolygon alloc] init];
}
#pragma mark Properties
- (void)setFrame:(CGRect)frame
{
[super setFrame:frame];
if (animating) {
[self stopAnimating];
[self beginAnimating];
}
}
- (BOOL)isAnimating
{
return animating;
}
- (void)setAnimationShape:(M13ProgressViewMetroAnimationShape)animationShape
{
_animationShape = animationShape;
if (_animationShape == M13ProgressViewMetroAnimationShapeEllipse) {
//Inset the size of the dot
CGFloat radius = MIN((self.bounds.size.width - _dotSize.width) / 2, (self.bounds.size.height - _dotSize.height) / 2);
//Create the path
animationPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.bounds.size.width / 2, self.bounds.size.height / 2) radius:radius startAngle:M_PI_2 endAngle:(-M_PI_2 * 3) clockwise:YES];
} else if (_animationShape == M13ProgressViewMetroAnimationShapeRectangle) {
//Inset the size of the dot
CGRect pathRect = CGRectInset(self.bounds, _dotSize.width, _dotSize.height);
//Create the path
animationPath = [UIBezierPath bezierPath];
[animationPath moveToPoint:CGPointMake((pathRect.size.width / 2) + pathRect.origin.x, pathRect.size.height + pathRect.origin.y)];
[animationPath addLineToPoint:CGPointMake(pathRect.origin.x, pathRect.size.height + pathRect.origin.y)];
[animationPath addLineToPoint:CGPointMake(pathRect.origin.x, pathRect.origin.y)];
[animationPath addLineToPoint:CGPointMake(pathRect.origin.x + pathRect.size.width, pathRect.origin.y)];
[animationPath addLineToPoint:CGPointMake(pathRect.origin.x + pathRect.size.width, pathRect.origin.y + pathRect.size.height)];
[animationPath moveToPoint:CGPointMake((pathRect.size.width / 2) + pathRect.origin.x, pathRect.size.height + pathRect.origin.y)];
} else if (animationShape == M13ProgressViewMetroAnimationShapeLine) {
//Create the path
animationPath = [UIBezierPath bezierPath];
[animationPath moveToPoint:CGPointMake(-_dotSize.width, self.bounds.size.height / 2)];
[animationPath addLineToPoint:CGPointMake(self.bounds.size.width + _dotSize.width, self.bounds.size.height / 2)];
}
[self stopAnimating];
[self beginAnimating];
}
- (void)setNumberOfDots:(NSUInteger)numberOfDots
{
_numberOfDots = numberOfDots;
[self invalidateIntrinsicContentSize];
[self stopAnimating];
[self beginAnimating];
}
- (void)setDotSize:(CGSize)dotSize
{
_dotSize = dotSize;
[self invalidateIntrinsicContentSize];
[self stopAnimating];
[self beginAnimating];
}
- (void)performAction:(M13ProgressViewAction)action animated:(BOOL)animated
{
for (M13ProgressViewMetroDot *dot in dots) {
[dot performAction:action animated:animated];
}
}
- (void)setProgress:(CGFloat)progress animated:(BOOL)animated
{
if (animated == NO) {
if (_displayLink) {
//Kill running animations
[_displayLink removeFromRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
_displayLink = nil;
}
[super setProgress:progress animated:NO];
[self showProgress];
} 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 showProgress];
return;
}
//Set progress
[super setProgress:_animationFromValue + dt * (_animationToValue - _animationFromValue) animated:YES];
[self showProgress];
});
}
- (void)showProgress
{
static int pastIndexToHighlightTo = 0;
int indexToHighlightTo = ceilf(_numberOfDots * self.progress);
//Only perform the animation if necessary.
if (pastIndexToHighlightTo != indexToHighlightTo) {
for (int i = 0; i < _numberOfDots; i++) {
M13ProgressViewMetroDot *dot = dots[i];
if (i <= indexToHighlightTo && self.progress != 0) {
dot.highlighted = YES;
} else {
dot.highlighted = NO;
}
}
pastIndexToHighlightTo = indexToHighlightTo;
}
}
- (CGSize)intrinsicContentSize
{
//No real constraint on size.
if (_animationShape == M13ProgressViewMetroAnimationShapeEllipse || _animationShape == M13ProgressViewMetroAnimationShapeRectangle) {
return CGSizeMake(3 * _dotSize.width, 3 * _dotSize.height);
} else {
return CGSizeMake(_dotSize.width * _numberOfDots, _dotSize.height);
}
}
#pragma mark Animation
-(void)beginAnimating
{
if (!animating){
animating = YES;
currentNumberOfDots = 0;
dots = [[NSMutableArray alloc] init];
//add circles
animationTimer = [NSTimer scheduledTimerWithTimeInterval: 0.20 target: self
selector: @selector(createCircle) userInfo: nil repeats: YES];
}
}
-(void)createCircle
{
if (currentNumberOfDots<_numberOfDots){
currentNumberOfDots ++;
CGRect f;
if (_animationShape != M13ProgressViewMetroAnimationShapeLine) {
f = CGRectMake((self.frame.size.width - _dotSize.width) / 2 - 1, self.frame.size.height - _dotSize.height - 1, _dotSize.width, _dotSize.height);
} else {
f = CGRectMake(- _dotSize.width, self.bounds.size.height / 2, _dotSize.width, _dotSize.height);
}
M13ProgressViewMetroDot *dotLayer = [_metroDot copy];
dotLayer.frame = f;
[self.layer addSublayer:dotLayer];
[dots addObject:dotLayer];
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
animation.duration = self.animationDuration;
animation.timingFunction = [CAMediaTimingFunction functionWithControlPoints:0.15f :0.60f :0.85f :0.4f];
[animation setCalculationMode:kCAAnimationPaced];
animation.path = animationPath.CGPath;
animation.repeatCount = HUGE_VALF;
if (_animationShape != M13ProgressViewMetroAnimationShapeLine) {
//No delay needed
[dotLayer addAnimation:animation forKey:@"metroAnimation"];
} else {
//Delay repeat
animation.repeatCount = 1;
CAAnimationGroup *group = [CAAnimationGroup animation];
group.duration = self.animationDuration * 2;
group.animations = @[animation];
group.repeatCount = HUGE_VALF;
[dotLayer addAnimation:group forKey:@"metroAnimation"];
}
} else {
[animationTimer invalidate];
}
}
-(void)stopAnimating
{
animating = NO;
[animationTimer invalidate];
for (CALayer *layer in dots) {
[layer removeAllAnimations];
[layer removeFromSuperlayer];
}
}
@end