-
Notifications
You must be signed in to change notification settings - Fork 0
/
DFCircleActivityIndicator.m
139 lines (107 loc) · 3.31 KB
/
DFCircleActivityIndicator.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
#import "DFCircleActivityIndicator.h"
@interface DFCircleActivityIndicator ()
@property (nonatomic, strong) UIBezierPath *circleBezier;
@property (nonatomic, strong) CAShapeLayer *shapeLayer;
@end
@implementation DFCircleActivityIndicator
#pragma mark -
#pragma mark Initialization
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self commonInit];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
[self commonInit];
}
return self;
}
- (void)commonInit {
[self initDefaults];
[self initShape];
}
#pragma mark -
#pragma mark Auxiliar Initialization
- (void)initDefaults {
_lineColor = [UIColor greenColor];
_rotationDuration = 1.0f;
_lineWidth = 15.0f;
_lineAlpha = 0.50f;
_emptySize = 0.20f;
_animating = NO;
}
- (void)initShape {
_circleBezier = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
_shapeLayer = [CAShapeLayer layer];
_shapeLayer.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
_shapeLayer.geometryFlipped = YES;
_shapeLayer.path = _circleBezier.CGPath;
_shapeLayer.strokeColor = [[_lineColor colorWithAlphaComponent:_lineAlpha] CGColor];
_shapeLayer.fillColor = nil;
_shapeLayer.lineWidth = 15.0f;
_shapeLayer.lineJoin = kCALineJoinRound;
_shapeLayer.strokeEnd = 0.82f;
[self.layer addSublayer:_shapeLayer];
}
#pragma mark -
#pragma mark Layout Handling
- (void)layoutSubviews {
[super layoutSubviews];
self.backgroundColor = [UIColor clearColor];
_shapeLayer.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
_shapeLayer.strokeColor = [[_lineColor colorWithAlphaComponent:_lineAlpha] CGColor];
_shapeLayer.lineWidth = _lineWidth;
_shapeLayer.strokeEnd = 1.0f - _emptySize;
_shapeLayer.path = _circleBezier.CGPath;
}
#pragma mark -
#pragma mark Public Methods
- (void)startAnimation {
if (!_animating) {
_animating = YES;
[self rotateWithOptions: UIViewAnimationOptionCurveEaseIn];
}
}
- (void)stopAnimation {
_animating = NO;
}
#pragma mark -
#pragma mark Animation Handling
- (void)rotateWithOptions:(UIViewAnimationOptions)options {
[UIView animateWithDuration:_rotationDuration / 4.0f delay:0.0f options:options
animations: ^{
self.transform = CGAffineTransformRotate(self.transform, M_PI / 2);
}
completion: ^(BOOL finished) {
if (finished) {
if (_animating) {
[self rotateWithOptions:UIViewAnimationOptionCurveLinear];
} else if (options != UIViewAnimationOptionCurveEaseOut) {
[self rotateWithOptions:UIViewAnimationOptionCurveEaseOut];
}
}
}];
}
#pragma mark -
#pragma mark Custom Setters
- (void)setLineColor:(UIColor *)lineColor {
_lineColor = lineColor;
[self setNeedsLayout];
}
- (void)setLineWidth:(CGFloat)lineWidth {
_lineWidth = lineWidth;
[self setNeedsLayout];
}
- (void)setEmptySize:(CGFloat)emptySize {
_emptySize = emptySize;
[self setNeedsLayout];
}
- (void)setRotationDuration:(CGFloat)rotationDuration {
_rotationDuration = MAX(0.01f, rotationDuration);
[self setNeedsLayout];
}
@end