This repository has been archived by the owner on Jul 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
PCAngularActivityIndicatorView.m
177 lines (139 loc) · 6.26 KB
/
PCAngularActivityIndicatorView.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
//
// PCAngularActivityIndicatorView.m
//
// Copyright (c) 2014 Phillip Caudell phillipcaudell@gmail.com
//
// The MIT License
//
// 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 "PCAngularActivityIndicatorView.h"
@interface PCAngularActivityIndicatorView ()
@property (nonatomic, strong) CAShapeLayer *shapeLayer;
@property (nonatomic, strong) UIView *contentView;
@property (nonatomic, assign) CGFloat duration;
@end
@implementation PCAngularActivityIndicatorView
- (id)init
{
return [self initWithActivityIndicatorStyle:PCAngularActivityIndicatorViewStyleDefault];
}
- (id)initWithCustomSize:(CGSize)size lineWidth:(CGFloat)lineWidth andDuration:(CGFloat)duration
{
CGRect frame = CGRectMake(0, 0, size.width, size.height);
if (self = [super initWithFrame:frame]) {
self.contentView = [[UIView alloc] initWithFrame:self.bounds];
[self addSubview:self.contentView];
CGFloat radius = frame.size.width / 2;
self.shapeLayer = [CAShapeLayer layer];
self.shapeLayer.frame = self.bounds;
self.shapeLayer.lineWidth = lineWidth;
self.shapeLayer.fillColor = [[UIColor clearColor] CGColor];
self.shapeLayer.path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 2.0 * radius, 2.0 * radius) cornerRadius:radius].CGPath;
self.shapeLayer.lineCap = kCALineJoinRound;
self.shapeLayer.hidden = YES;
[self.contentView.layer insertSublayer:self.shapeLayer atIndex:0];
// Defaults
self.hidesWhenStopped = YES;
self.duration = duration;
self.color = [UIColor lightGrayColor];
}
return self;
}
- (id)initWithActivityIndicatorStyle:(PCAngularActivityIndicatorViewStyle)style
{
CGRect frame;
CGFloat lineWidth;
CGFloat duration;
switch (style) {
case PCAngularActivityIndicatorViewStyleSmall:
frame = CGRectMake(0, 0, 20, 20);
lineWidth = 2.0;
duration = 0.8;
break;
case PCAngularActivityIndicatorViewStyleDefault:
frame = CGRectMake(0, 0, 30, 30);
lineWidth = 4.0;
duration = 0.8;
break;
case PCAngularActivityIndicatorViewStyleLarge:
frame = CGRectMake(0, 0, 60, 60);
lineWidth = 8.0;
duration = 1.0;
break;
default:
break;
}
if (self = [super initWithFrame:frame]) {
self.contentView = [[UIView alloc] initWithFrame:self.bounds];
[self addSubview:self.contentView];
CGFloat radius = frame.size.width / 2;
self.shapeLayer = [CAShapeLayer layer];
self.shapeLayer.frame = self.bounds;
self.shapeLayer.lineWidth = lineWidth;
self.shapeLayer.fillColor = [[UIColor clearColor] CGColor];
self.shapeLayer.path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 2.0 * radius, 2.0 * radius) cornerRadius:radius].CGPath;
self.shapeLayer.lineCap = kCALineJoinRound;
self.shapeLayer.hidden = YES;
[self.contentView.layer insertSublayer:self.shapeLayer atIndex:0];
// Defaults
self.hidesWhenStopped = YES;
self.duration = duration;
self.color = [UIColor lightGrayColor];
}
return self;
}
- (void)startAnimating
{
if (self.isAnimating) {
return;
}
_animating = YES;
CAKeyframeAnimation *inAnimation = [CAKeyframeAnimation animationWithKeyPath:@"strokeEnd"];
inAnimation.duration = self.duration;
inAnimation.values = @[@(0), @(1)];
CAKeyframeAnimation *outAnimation = [CAKeyframeAnimation animationWithKeyPath:@"strokeStart"];
outAnimation.duration = self.duration;
outAnimation.values = @[@(0), @(0.8), @(1)];
outAnimation.beginTime = self.duration / 1.5;
CAAnimationGroup *groupAnimation = [CAAnimationGroup animation];
groupAnimation.animations = @[inAnimation, outAnimation];
groupAnimation.duration = self.duration + outAnimation.beginTime;
groupAnimation.repeatCount = INFINITY;
groupAnimation.delegate = self;
CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
rotationAnimation.fromValue = @(0);
rotationAnimation.toValue = @(M_PI * 2);
rotationAnimation.duration = self.duration * 1.5;
rotationAnimation.repeatCount = INFINITY;
[self.shapeLayer addAnimation:rotationAnimation forKey:nil];
[self.shapeLayer addAnimation:groupAnimation forKey:nil];
self.shapeLayer.hidden = NO;
}
- (void)stopAnimating
{
[UIView animateWithDuration:0.5 animations:^{
// Nice fade and ride
self.contentView.transform = CGAffineTransformMakeScale(1.2, 1.2);
self.contentView.alpha = 0.0;
} completion:^(BOOL finished) {
_animating = NO;
/// ...and reset back
self.contentView.transform = CGAffineTransformMakeScale(1.0, 1.0);
self.contentView.alpha = 1.0;
self.shapeLayer.hidden = self.hidesWhenStopped;
[self.shapeLayer removeAllAnimations];
}];
}
- (void)setColor:(UIColor *)color
{
[self willChangeValueForKey:@"color"];
_color = color;
self.shapeLayer.strokeColor = [color CGColor];
[self didChangeValueForKey:@"color"];
}
@end