-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathDCProgressView.m
219 lines (195 loc) · 8.52 KB
/
DCProgressView.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
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// DCProgressView.m
// iOSTester
//
// Created by Dalton Cherry on 4/20/13.
// Copyright 2013 Basement Krew. All rights reserved.
//
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#import "DCProgressView.h"
#import <math.h>
@implementation DCProgressView
@synthesize tintColor,borderColor,borderWidth,fillColor,corners,rounding,progress;
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
[self sharedInit];
}
return self;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self sharedInit];
}
return self;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- (void)sharedInit
{
self.borderWidth = 1;
self.rounding = 4;
self.corners = UIRectCornerAllCorners;
self.backgroundColor = [UIColor clearColor];
animateProgress = -1;
self.tintColor = [UIColor colorWithRed:0/255.0f green:136/255.0f blue:204/255.0f alpha:1];
// Initialization code
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- (void)drawRect:(CGRect)rect
{
if(!self.fillColor)
self.fillColor = [UIColor colorWithWhite:0.97 alpha:1];
if(!self.borderColor)
self.borderColor = [UIColor colorWithWhite:0.90 alpha:1];
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGRect frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
//draw the border
if(self.borderWidth > 0)
{
frame = CGRectInset(frame, 0.5, 0.5);
CGContextSaveGState(ctx);
CGContextSetStrokeColorWithColor(ctx, self.borderColor.CGColor);
CGContextSetLineWidth(ctx, self.borderWidth);
CGContextSetLineJoin(ctx, kCGLineJoinRound);
CGPathRef path = [UIBezierPath bezierPathWithRoundedRect:frame
byRoundingCorners:self.corners
cornerRadii:CGSizeMake(self.rounding, self.rounding)].CGPath;
CGContextAddPath(ctx, path);
CGContextStrokePath(ctx);
CGContextRestoreGState(ctx);
}
// draws background
CGContextSaveGState(ctx);
CGPathRef path = [UIBezierPath bezierPathWithRoundedRect:frame
byRoundingCorners:self.corners
cornerRadii:CGSizeMake(self.rounding, self.rounding)].CGPath;
CGContextAddPath(ctx, path);
CGContextClip(ctx);
NSArray* newGradientColors = [NSArray arrayWithObjects:
(id)[UIColor colorWithWhite:0.92 alpha:1].CGColor,
(id)self.fillColor.CGColor,
(id)self.fillColor.CGColor, nil];
CGFloat newGradientLocations[] = {0.0,0.12, 1.0};
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef)newGradientColors, newGradientLocations);
CGContextDrawLinearGradient(ctx, gradient, CGPointMake(self.borderWidth, 0), CGPointMake(self.borderWidth, frame.size.height), 0);
CGGradientRelease(gradient);
CGColorSpaceRelease(colorSpace);
CGContextRestoreGState(ctx);
//draw the fill
CGContextSaveGState(ctx);
float current = animateProgress;
if(current <= -1)
current = self.progress;
CGRect tintFrame = frame;
tintFrame.size.width = tintFrame.size.width*current;
UIRectCorner tintCorners = self.corners;
if(current+0.02 < 1 && self.corners & UIRectCornerAllCorners)
tintCorners = UIRectCornerBottomLeft | UIRectCornerTopLeft;
path = [UIBezierPath bezierPathWithRoundedRect:tintFrame
byRoundingCorners:tintCorners
cornerRadii:CGSizeMake(self.rounding, self.rounding)].CGPath;
CGContextAddPath(ctx, path);
CGContextClip(ctx);
NSArray* fillGradientColors = [NSArray arrayWithObjects:
(id)self.tintColor.CGColor,
(id)[self adjustColor:self.tintColor point:-0.12].CGColor, nil];
CGFloat fillGradientLocations[] = {0.0, 1.0};
colorSpace = CGColorSpaceCreateDeviceRGB();
gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef)fillGradientColors, fillGradientLocations);
CGContextDrawLinearGradient(ctx, gradient, CGPointMake(0, 0), CGPointMake(0, frame.size.height+self.borderWidth), 0);
CGGradientRelease(gradient);
CGColorSpaceRelease(colorSpace);
CGContextRestoreGState(ctx);
if(animateProgress > -1 && (animateProgress+0.01 < self.progress || animateProgress-0.01 > self.progress) )
{
if(animateProgress < self.progress)
animateProgress += 0.01;
else
animateProgress -= 0.01;
[self performSelector:@selector(reloadView) withObject:nil afterDelay:0.01];
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-(void)setProgress:(CGFloat)pro
{
if(pro > 1)
pro = 1;
if(pro < 0)
pro = 0;
animateProgress = -1;
progress = floor(pro * 100) / 100;
[self setNeedsDisplay];
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-(void)setProgress:(CGFloat)pro animated:(BOOL)animated
{
if(pro > 1)
pro = 1;
if(pro < 0)
pro = 0;
if(animated)
animateProgress = progress;
else
animateProgress = -1;
progress = floor(pro * 100) / 100;
[self setNeedsDisplay];
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-(void)reloadView
{
[self setNeedsDisplay];
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-(UIColor*)adjustColor:(UIColor*)color point:(CGFloat)point
{
int totalComponents = CGColorGetNumberOfComponents(color.CGColor);
BOOL isGreyscale = totalComponents == 2 ? YES : NO;
CGFloat* oldComponents = (CGFloat*)CGColorGetComponents(color.CGColor);
CGFloat newComponents[4];
if (isGreyscale)
{
newComponents[0] = [self checkValue:oldComponents[0]+point point:point];
newComponents[1] = [self checkValue:oldComponents[0]+point point:point];
newComponents[2] = [self checkValue:oldComponents[0]+point point:point];
newComponents[3] = oldComponents[1];
}
else
{
newComponents[0] = [self checkValue:oldComponents[0]+point point:point];
newComponents[1] = [self checkValue:oldComponents[1]+point point:point];
newComponents[2] = [self checkValue:oldComponents[2]+point point:point];
newComponents[3] = oldComponents[3];
}
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGColorRef newColor = CGColorCreate(colorSpace, newComponents);
CGColorSpaceRelease(colorSpace);
UIColor *retColor = [UIColor colorWithCGColor:newColor];
CGColorRelease(newColor);
return retColor;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-(CGFloat)checkValue:(CGFloat)current point:(CGFloat)point
{
if(point < 0)
return [self checkNeg:current];
return [self checkPos:current];
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-(CGFloat)checkPos:(CGFloat)current
{
return current > 1.0 ? 1.0 : current;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-(CGFloat)checkNeg:(CGFloat)current
{
return current < 0.0 ? 0.0 : current;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@end