Repository navigation
Expand file tree
/
Copy pathJGProgressHUDPieIndicatorView.m
More file actions
executable file
·171 lines (116 loc) · 4.48 KB
/
Copy pathJGProgressHUDPieIndicatorView.m
File metadata and controls
executable file
·171 lines (116 loc) · 4.48 KB
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
//
// JGProgressHUDPieIndicatorView.m
// JGProgressHUD
//
// Created by Jonas Gessner on 19.07.14.
// Copyright (c) 2014 Jonas Gessner. All rights reserved.
//
#import "JGProgressHUDPieIndicatorView.h"
@interface JGProgressHUDPieIndicatorLayer : CALayer
@property (nonatomic, assign) float progress;
@property (nonatomic, weak) UIColor *color;
@property (nonatomic, weak) UIColor *fillColor;
@end
@implementation JGProgressHUDPieIndicatorLayer
@dynamic progress, color, fillColor;
+ (BOOL)needsDisplayForKey:(NSString *)key {
return ([key isEqualToString:@"progress"] || [key isEqualToString:@"color"] || [key isEqualToString:@"fillColor"] || [super needsDisplayForKey:key]);
}
- (id <CAAction>)actionForKey:(NSString *)key {
if ([key isEqualToString:@"progress"]) {
CABasicAnimation *progressAnimation = [CABasicAnimation animation];
progressAnimation.fromValue = [self.presentationLayer valueForKey:key];
progressAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
return progressAnimation;
}
return [super actionForKey:key];
}
- (void)drawInContext:(CGContextRef)ctx {
UIGraphicsPushContext(ctx);
CGRect rect = self.bounds;
CGPoint center = CGPointMake(rect.origin.x + (CGFloat)floor(rect.size.width/2.0), rect.origin.y + (CGFloat)floor(rect.size.height/2.0));
CGFloat lineWidth = 2.0;
CGFloat radius = (CGFloat)floor(MIN(rect.size.width, rect.size.height)/2.0)-lineWidth;
UIBezierPath *borderPath = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:0.0 endAngle:2.0*(CGFloat)M_PI clockwise:NO];
[borderPath setLineWidth:lineWidth];
if (self.fillColor) {
[self.fillColor setFill];
[borderPath fill];
}
[self.color set];
[borderPath stroke];
if (self.progress > 0.0) {
UIBezierPath *processPath = [UIBezierPath bezierPath];
[processPath setLineWidth:radius];
CGFloat startAngle = -((CGFloat)M_PI/2.0);
CGFloat endAngle = startAngle + 2.0 * (CGFloat)M_PI * self.progress;
[processPath addArcWithCenter:center radius:radius/2.0 startAngle:startAngle endAngle:endAngle clockwise:YES];
[processPath stroke];
}
UIGraphicsPopContext();
}
@end
@implementation JGProgressHUDPieIndicatorView
#pragma mark - Initializers
- (instancetype)init {
self = [super initWithContentView:nil];
if (self) {
self.layer.contentsScale = [UIScreen mainScreen].scale;
[self.layer setNeedsDisplay];
self.color = [UIColor clearColor];
self.fillColor = [UIColor clearColor];
}
return self;
}
- (instancetype)initWithHUDStyle:(JGProgressHUDStyle)style {
return [self init];
}
- (instancetype)initWithContentView:(UIView *)contentView {
return [self init];
}
#pragma mark - Getters & Setters
- (void)setColor:(UIColor *)tintColor {
if ([tintColor isEqual:self.color]) {
return;
}
_color = tintColor;
[(JGProgressHUDPieIndicatorLayer *)self.layer setColor:self.color];
}
- (void)setFillColor:(UIColor *)fillColor {
if ([fillColor isEqual:self.fillColor]) {
return;
}
_fillColor = fillColor;
[(JGProgressHUDPieIndicatorLayer *)self.layer setFillColor:self.fillColor];
}
- (void)setProgress:(float)progress animated:(BOOL)animated {
if (fequal(self.progress, progress)) {
return;
}
[super setProgress:progress animated:animated];
[CATransaction begin];
[CATransaction setAnimationDuration:(animated ? 0.3 : 0.0)];
[(JGProgressHUDPieIndicatorLayer *)self.layer setProgress:progress];
[CATransaction commit];
}
#pragma mark - Overrides
- (void)setUpForHUDStyle:(JGProgressHUDStyle)style vibrancyEnabled:(BOOL)vibrancyEnabled {
[super setUpForHUDStyle:style vibrancyEnabled:vibrancyEnabled];
if (style == JGProgressHUDStyleDark) {
self.color = [UIColor colorWithWhite:1.0 alpha:1.0];
self.fillColor = [UIColor colorWithWhite:0.2 alpha:1.0];
}
else {
self.color = [UIColor blackColor];
if (style == JGProgressHUDStyleLight) {
self.fillColor = [UIColor colorWithWhite:0.85 alpha:1.0];
}
else {
self.fillColor = [UIColor colorWithWhite:0.9 alpha:1.0];
}
}
}
+ (Class)layerClass {
return [JGProgressHUDPieIndicatorLayer class];
}
@end