forked from yannickl/YLProgressBar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
YLProgressBar.m
365 lines (294 loc) · 13.1 KB
/
YLProgressBar.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
/*
* YLProgressBar.m
*
* Copyright 2012 Yannick Loriot.
* http://yannickloriot.com
*
* 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 "YLProgressBar.h"
#import "ARCMacro.h"
// Sizes
#define YLProgressBarSizeInset 1
#define YLProgressBarSizeStripeWidth 7
// Colors
#define YLProgressBarColorBackground [UIColor colorWithRed:0.0980f green:0.1137f blue:0.1294f alpha:1.0f]
#define YLProgressBarColorBackgroundGlow [UIColor colorWithRed:0.0666f green:0.0784f blue:0.0901f alpha:1.0f]
@interface YLProgressBar ()
{
UIColor* _progressTintColor;
UIColor* _progressTintColorDark;
}
@property (nonatomic, assign) double progressOffset;
@property (nonatomic, assign) CGFloat cornerRadius;
@property (nonatomic, SAFE_ARC_PROP_RETAIN) NSTimer* animationTimer;
/** Init the progress bar. */
- (void)initializeProgressBar;
/** Build the stripes. */
- (UIBezierPath *)stripeWithOrigin:(CGPoint)origin bounds:(CGRect)frame;
/** Draw the background (track) of the slider. */
- (void)drawBackgroundWithRect:(CGRect)rect;
/** Draw the progress bar. */
- (void)drawProgressBarWithRect:(CGRect)rect;
/** Draw the gloss into the given rect. */
- (void)drawGlossWithRect:(CGRect)rect;
/** Draw the stipes into the given rect. */
- (void)drawStripesWithRect:(CGRect)rect;
@end
@implementation YLProgressBar
@synthesize progressOffset, cornerRadius, animationTimer;
@synthesize animated;
- (void)dealloc
{
if (animationTimer && [animationTimer isValid])
{
[animationTimer invalidate];
}
SAFE_ARC_RELEASE (animationTimer);
SAFE_ARC_RELEASE (_progressTintColor);
SAFE_ARC_RELEASE (_progressTintColorDark);
SAFE_ARC_SUPER_DEALLOC ();
}
-(id)initWithFrame:(CGRect)frameRect
{
if ((self = [super initWithFrame:frameRect]))
{
[self initializeProgressBar];
}
return self;
}
- (void)awakeFromNib
{
[super awakeFromNib];
[self initializeProgressBar];
}
- (void)drawRect:(CGRect)rect
{
// Refresh the corner radius value
self.cornerRadius = rect.size.height / 2;
// Compute the progressOffset for the animation
self.progressOffset = (self.progressOffset > 2 * YLProgressBarSizeStripeWidth - 1) ? 0 : ++self.progressOffset;
// Draw the background track
// [self drawBackgroundWithRect:rect];
if (self.progress > 0)
{
CGRect innerRect = CGRectMake(YLProgressBarSizeInset,
YLProgressBarSizeInset,
rect.size.width * self.progress - 2 * YLProgressBarSizeInset,
rect.size.height - 2 * YLProgressBarSizeInset);
[self drawProgressBarWithRect:innerRect];
[self drawStripesWithRect:innerRect];
[self drawGlossWithRect:innerRect];
}
}
- (void)setProgressTintColor:(UIColor *)aProgressTintColor
{
SAFE_ARC_RELEASE(_progressTintColor);
_progressTintColor = SAFE_ARC_RETAIN(aProgressTintColor);
const CGFloat* components = CGColorGetComponents(_progressTintColor.CGColor);
_progressTintColorDark = SAFE_ARC_RETAIN([UIColor colorWithRed:components[0] / 1.5f
green:components[1] / 1.5f
blue:components[2] / 1.5f
alpha:CGColorGetAlpha(_progressTintColor.CGColor)]);
}
- (UIColor*)progressTintColor
{
if (!_progressTintColor)
{
[self setProgressTintColor:[UIColor purpleColor]];
}
return _progressTintColor;
}
#pragma mark -
#pragma mark YLProgressBar Public Methods
- (void)setAnimated:(BOOL)_animated
{
animated = _animated;
if (animated)
{
if (self.animationTimer == nil)
{
self.animationTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/30
target:self
selector:@selector(setNeedsDisplay)
userInfo:nil
repeats:YES];
}
} else
{
if (self.animationTimer && [animationTimer isValid])
{
[animationTimer invalidate];
}
self.animationTimer = nil;
}
}
#pragma mark YLProgressBar Private Methods
- (void)initializeProgressBar
{
self.progressOffset = 0;
self.animationTimer = nil;
self.animated = YES;
}
- (UIBezierPath *)stripeWithOrigin:(CGPoint)origin bounds:(CGRect)frame
{
float height = frame.size.height;
UIBezierPath *rect = [UIBezierPath bezierPath];
[rect moveToPoint:origin];
[rect addLineToPoint:CGPointMake(origin.x + YLProgressBarSizeStripeWidth, origin.y)];
[rect addLineToPoint:CGPointMake(origin.x + YLProgressBarSizeStripeWidth - 8, origin.y + height)];
[rect addLineToPoint:CGPointMake(origin.x - 8, origin.y + height)];
[rect addLineToPoint:origin];
[rect closePath];
return rect;
}
- (void)drawBackgroundWithRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
{
// Draw the white shadow
[[UIColor colorWithRed:1.0f green:1.0f blue:1.0f alpha:0.2] set];
UIBezierPath* shadow = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0.5, 0, rect.size.width - 1, rect.size.height - 1)
cornerRadius:cornerRadius];
[shadow stroke];
// Draw the track
[YLProgressBarColorBackground set];
UIBezierPath* roundedRect = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, rect.size.width, rect.size.height-1) cornerRadius:cornerRadius];
[roundedRect fill];
// Draw the inner glow
[YLProgressBarColorBackgroundGlow set];
CGMutablePathRef glow = CGPathCreateMutable();
CGPathMoveToPoint(glow, NULL, cornerRadius, 0);
CGPathAddLineToPoint(glow, NULL, rect.size.width - cornerRadius, 0);
CGContextAddPath(context, glow);
CGContextDrawPath(context, kCGPathStroke);
CGPathRelease(glow);
}
CGContextRestoreGState(context);
}
- (void)drawProgressBarWithRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextSaveGState(context);
{
UIBezierPath *progressBounds = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:cornerRadius];
CGContextAddPath(context, [progressBounds CGPath]);
CGContextClip(context);
// size_t num_locations = 2;
CGFloat locations[] = {0.0, 1.0};
CFArrayRef colors = (__bridge CFArrayRef) [NSArray arrayWithObjects:(id)_progressTintColorDark.CGColor,
(id)self.progressTintColor.CGColor,
nil];
CGGradientRef gradient = CGGradientCreateWithColors (colorSpace, colors, locations);
CGContextDrawLinearGradient(context, gradient, CGPointMake(rect.origin.x, rect.origin.y), CGPointMake(rect.origin.x + rect.size.width, rect.origin.y), (kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation));
CGGradientRelease(gradient);
}
CGContextRestoreGState(context);
CGColorSpaceRelease(colorSpace);
}
- (void)drawGlossWithRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextSaveGState(context);
{
// Draw the gloss
CGContextSetBlendMode(context, kCGBlendModeOverlay);
CGContextBeginTransparencyLayerWithRect(context, CGRectMake(rect.origin.x, rect.origin.y + floorf(rect.size.height) / 2, rect.size.width, floorf(rect.size.height) / 2), NULL);
{
const CGFloat glossGradientComponents[] = {1.0f, 1.0f, 1.0f, 0.47f, 0.0f, 0.0f, 0.0f, 0.0f};
const CGFloat glossGradientLocations[] = {1.0, 0.0};
CGGradientRef glossGradient = CGGradientCreateWithColorComponents(colorSpace, glossGradientComponents, glossGradientLocations, (kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation));
CGContextDrawLinearGradient(context, glossGradient, CGPointMake(0, 0), CGPointMake(0, rect.size.width), 0);
CGGradientRelease(glossGradient);
}
CGContextEndTransparencyLayer(context);
// Draw the gloss's drop shadow
CGContextSetBlendMode(context, kCGBlendModeSoftLight);
CGContextBeginTransparencyLayer(context, NULL);
{
CGRect fillRect = CGRectMake(rect.origin.x, rect.origin.y + floorf(rect.size.height / 2), rect.size.width, floorf(rect.size.height / 2));
const CGFloat glossDropShadowComponents[] = {0.0f, 0.0f, 0.0f, 0.16f, 0.0f, 0.0f, 0.0f, 0.0f};
CGColorRef glossDropShadowColor = CGColorCreate(colorSpace, glossDropShadowComponents);
CGContextSaveGState(context);
{
CGContextSetShadowWithColor(context, CGSizeMake(0, -1), 4, glossDropShadowColor);
CGContextFillRect(context, fillRect);
CGColorRelease(glossDropShadowColor);
}
CGContextRestoreGState(context);
CGContextSetBlendMode(context, kCGBlendModeClear);
CGContextFillRect(context, fillRect);
}
CGContextEndTransparencyLayer(context);
}
CGContextRestoreGState(context);
UIBezierPath *progressBounds = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:cornerRadius];
// Draw progress bar glow
CGContextSaveGState(context);
{
CGContextAddPath(context, [progressBounds CGPath]);
const CGFloat progressBarGlowComponents[] = {1.0f, 1.0f, 1.0f, 0.12f};
CGColorRef progressBarGlowColor = CGColorCreate(colorSpace, progressBarGlowComponents);
CGContextSetBlendMode(context, kCGBlendModeOverlay);
CGContextSetStrokeColorWithColor(context, progressBarGlowColor);
CGContextSetLineWidth(context, 2.0f);
CGContextStrokePath(context);
CGColorRelease(progressBarGlowColor);
}
CGContextRestoreGState(context);
CGColorSpaceRelease(colorSpace);
}
- (void)drawStripesWithRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB ();
CGContextSaveGState(context);
{
UIBezierPath *allStripes = [UIBezierPath bezierPath];
for (int i = 0; i <= rect.size.width / (2 * YLProgressBarSizeStripeWidth) + (2 * YLProgressBarSizeStripeWidth); i++)
{
UIBezierPath* stripe = [self stripeWithOrigin:CGPointMake(i * 2 * YLProgressBarSizeStripeWidth + self.progressOffset, YLProgressBarSizeInset)
bounds:rect];
[allStripes appendPath:stripe];
}
// Clip the progress frame
UIBezierPath *clipPath = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:cornerRadius];
CGContextAddPath(context, [clipPath CGPath]);
CGContextClip(context);
CGContextSaveGState(context);
{
// Clip the stripes
CGContextAddPath(context, [allStripes CGPath]);
CGContextClip(context);
const CGFloat stripesColorComponents[] = { 0.0f, 0.0f, 0.0f, 0.28f };
CGColorRef stripesColor = CGColorCreate(colorSpace, stripesColorComponents);
CGContextSetFillColorWithColor(context, stripesColor);
CGContextFillRect(context, rect);
CGColorRelease(stripesColor);
}
CGContextRestoreGState(context);
}
CGContextRestoreGState(context);
CGColorSpaceRelease(colorSpace);
}
@end