-
Notifications
You must be signed in to change notification settings - Fork 0
/
ColorChangingBarProgressView.m
202 lines (146 loc) · 6.22 KB
/
ColorChangingBarProgressView.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
// BarProgressView.m
// BarProgressView
//
// modified by behlul ucar
// based on initial work of jonathan king
#import "ColorChangingBarProgressView.h"
@interface ColorChangingBarProgressView() {
//The layer which will contain the bars
CALayer *progressLayer;
int totalNumberOfBars;
}
// Private method which calculates how many bars should be drawn
- (int)numberOfBarsForWidth:(CGFloat)width;
// Private method which calculates how much left over space ther will be, as the bars width might not exactly qual the screen width, so padding on either side may be necesssary.
- (float)remainderFromBarsInWidth:(CGFloat)width;
// Private methods which create the bars
- (CALayer *)completeBarLayerOfSize:(CGRect)size color:(UIColor*)color;
- (CALayer *)emptyBarLayerOfSize:(CGRect)size;
@end
@implementation ColorChangingBarProgressView
@synthesize progress = _progress;
@synthesize widthOfBar = _widthOfBar;
@synthesize completeBarStartColor, completeBarEndColor, emptyBarFillColor;
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
[self doInit];
}
return self;
}
-(id)init {
if (self = [super init]) {
[self doInit];
}
return self;
}
-(id)initWithCoder:(NSCoder *)aDecoder {
if (self = [super initWithCoder:aDecoder]) {
[self doInit];
}
return self;
}
-(void)doInit {
self.backgroundColor = [UIColor clearColor];
//Set the fill colors to the default values
self.completeBarStartColor = defaultCompleteBarStartColor;
self.completeBarEndColor = defaultCompleteBarEndColor;
self.emptyBarFillColor = defaultEmptyBarFillColor;
//Set the default width of the bars.
self.widthOfBar = defaultWidthOfBar;
self.progress = 0;
}
- (void) drawRect:(CGRect)rect
{
[super drawRect:rect];
// Drawing clear background.
// [[UIColor clearColor] set];
// UIRectFill(rect);
//Create containing layer for the bars.
progressLayer = [CALayer layer];
[progressLayer setFrame:rect];
[progressLayer setBackgroundColor:[[UIColor colorWithWhite:1.0f alpha:0.0f] CGColor]];
[self.layer addSublayer:progressLayer];
}
- (void)layoutSubviews {
//As the width may have changed,
//When the height or width of the frame is changed, the bars are redrawn.
[self redrawBars];
}
- (void)redrawBars {
// //Information about the bars being drawn
// int remainder = [self remainderFromBarsForWidth:self.frame.size.width];
// NSLog(@"Total number of bars: %d - With a padding of %d", totalNumberOfBars, remainder);
float progress = _progress;
totalNumberOfBars = [self numberOfBarsForWidth:self.frame.size.width];
int numberOfCompleteBars = totalNumberOfBars * progress;
float remainderOfWidth = [self remainderFromBarsInWidth:self.frame.size.width]/2.0f;
progressLayer.sublayers = nil; //Remove all the sublayers
for (int i = 0; i < numberOfCompleteBars ; i++) {
float XPositionForBar = (_widthOfBar*i + defaultSpaceBetweenBars*(i-1))+remainderOfWidth;
float heightForBar = self.frame.size.height;
CGFloat sHue, sSat, sBright, sAlpha;
CGFloat eHue, eSat, eBright, eAlpha;
[self.completeBarStartColor getHue:&sHue saturation:&sSat brightness:&sBright alpha:&sAlpha];
[self.completeBarEndColor getHue:&eHue saturation:&eSat brightness:&eBright alpha:&eAlpha];
UIColor* barColor = [UIColor colorWithHue:sHue + (eHue-sHue)*((float)i/totalNumberOfBars) saturation:sSat brightness:sBright alpha:sAlpha];
[progressLayer addSublayer:[self completeBarLayerOfSize:
CGRectMake(XPositionForBar, 0, _widthOfBar, heightForBar)
color:barColor]];
}
for (int i = numberOfCompleteBars; i < totalNumberOfBars ; i++) {
float XPositionForBar = (_widthOfBar*i + defaultSpaceBetweenBars*(i-1))+remainderOfWidth;
float heightForBar = self.frame.size.height;
[progressLayer addSublayer:[self emptyBarLayerOfSize:CGRectMake(XPositionForBar, 0, _widthOfBar, heightForBar)]];
}
}
#pragma mark Setting Atrributes
- (void)setProgress:(float)progress {
float currentProgress = _progress;
float futureProgress = progress;
//If progress is greater than 1, make it equal 1 - Failsafe, which stops crashing.
if(progress<=1) _progress = progress;
else _progress = 1;
//Work out the current number of complete bars as integers, and compare this number to that with the future number of complete bars. If the two numbers are different redraw the nars, otherwise do nothing. By doing this, it is saving uneccasary calls for redrawing, and improving performance.
int currentNumberOfCompleteBars = totalNumberOfBars * currentProgress;
int futureNumberOfCompleteBars = totalNumberOfBars * futureProgress;
if (currentNumberOfCompleteBars!=futureNumberOfCompleteBars) {
//Redraw the bars with the new progress data.
[self redrawBars];
}
}
- (void)setWidthOfBar:(int)widthOfBar {
_widthOfBar = widthOfBar;
//Redraw the bars with the new width.
[self redrawBars];
}
#pragma mark Bar Creation
- (CALayer *)completeBarLayerOfSize:(CGRect)size
color:(UIColor*)color {
CALayer *layer = [CALayer layer];
layer.frame = size;
[layer setBackgroundColor:color.CGColor];
return layer;
}
- (CALayer *)emptyBarLayerOfSize:(CGRect)size {
CALayer *layer = [CALayer layer];
layer.frame = size;
[layer setBackgroundColor:[emptyBarFillColor CGColor]];
return layer;
}
#pragma mark Bar Logic
- (int)numberOfBarsForWidth:(CGFloat)width {
int numberOfBars; numberOfBars = 1;
while ((_widthOfBar*numberOfBars + defaultSpaceBetweenBars*(numberOfBars-1)) <= width) {
numberOfBars ++;
}
return numberOfBars - 1; //As the while statment stops when numberOfBars is greater, the number - 1 would be the last possible value.
}
- (float)remainderFromBarsInWidth:(CGFloat)width {
CGFloat totalWidthOfProgressBar; totalWidthOfProgressBar = _widthOfBar*totalNumberOfBars + defaultSpaceBetweenBars*(totalNumberOfBars-1);
return width - totalWidthOfProgressBar;
}
- (void)dealloc {
[progressLayer removeAllAnimations];
[progressLayer removeFromSuperlayer];
}
@end