Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
805 | - | 1 | // |
2 | // ButtonGradientView.m |
||
3 | // Custom Alert View |
||
4 | // |
||
5 | // Created by jeff on 5/17/10. |
||
6 | // Copyright 2010 __MyCompanyName__. All rights reserved. |
||
7 | // |
||
8 | |||
9 | #import "GradientButton.h" |
||
10 | |||
11 | @interface GradientButton() |
||
12 | @property (nonatomic, readonly) CGGradientRef normalGradient; |
||
13 | @property (nonatomic, readonly) CGGradientRef highlightGradient; |
||
14 | - (void)hesitateUpdate; // Used to catch and fix problem where quick taps don't get updated back to normal state |
||
15 | @end |
||
16 | #pragma mark - |
||
17 | |||
18 | @implementation GradientButton |
||
19 | @synthesize normalGradientColors; |
||
20 | @synthesize normalGradientLocations; |
||
21 | @synthesize highlightGradientColors; |
||
22 | @synthesize highlightGradientLocations; |
||
23 | @synthesize cornerRadius; |
||
24 | @synthesize normalGradient, highlightGradient; |
||
25 | #pragma mark - |
||
26 | - (CGGradientRef)normalGradient |
||
27 | { |
||
28 | if (normalGradient == NULL) |
||
29 | { |
||
30 | int locCount = [normalGradientLocations count]; |
||
31 | CGFloat locations[locCount]; |
||
32 | for (int i = 0; i < [normalGradientLocations count]; i++) |
||
33 | { |
||
34 | NSNumber *location = [normalGradientLocations objectAtIndex:i]; |
||
35 | locations[i] = [location floatValue]; |
||
36 | } |
||
37 | CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB(); |
||
38 | |||
39 | normalGradient = CGGradientCreateWithColors(space, (CFArrayRef)normalGradientColors, locations); |
||
40 | CGColorSpaceRelease(space); |
||
41 | } |
||
42 | return normalGradient; |
||
43 | } |
||
44 | - (CGGradientRef)highlightGradient |
||
45 | { |
||
46 | |||
47 | if (highlightGradient == NULL) |
||
48 | { |
||
49 | CGFloat locations[[highlightGradientLocations count]]; |
||
50 | for (int i = 0; i < [highlightGradientLocations count]; i++) |
||
51 | { |
||
52 | NSNumber *location = [highlightGradientLocations objectAtIndex:i]; |
||
53 | locations[i] = [location floatValue]; |
||
54 | } |
||
55 | CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB(); |
||
56 | |||
57 | highlightGradient = CGGradientCreateWithColors(space, (CFArrayRef)highlightGradientColors, locations); |
||
58 | CGColorSpaceRelease(space); |
||
59 | } |
||
60 | return highlightGradient; |
||
61 | } |
||
62 | #pragma mark - |
||
63 | - (id)initWithFrame:(CGRect)frame |
||
64 | { |
||
65 | self = [super initWithFrame:frame]; |
||
66 | if (self) |
||
67 | { |
||
68 | [self setOpaque:NO]; |
||
69 | self.backgroundColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.0]; |
||
70 | } |
||
71 | return self; |
||
72 | } |
||
73 | #pragma mark - |
||
74 | #pragma mark Appearances |
||
75 | - (void)useAlertStyle |
||
76 | { |
||
77 | |||
78 | NSMutableArray *colors = [NSMutableArray arrayWithCapacity:3]; |
||
79 | UIColor *color = [UIColor colorWithRed:0.283 green:0.32 blue:0.414 alpha:1.0]; |
||
80 | [colors addObject:(id)[color CGColor]]; |
||
81 | color = [UIColor colorWithRed:0.82 green:0.834 blue:0.87 alpha:1.0]; |
||
82 | [colors addObject:(id)[color CGColor]]; |
||
83 | color = [UIColor colorWithRed:0.186 green:0.223 blue:0.326 alpha:1.0]; |
||
84 | [colors addObject:(id)[color CGColor]]; |
||
85 | self.normalGradientColors = colors; |
||
86 | self.normalGradientLocations = [NSMutableArray arrayWithObjects: |
||
87 | [NSNumber numberWithFloat:0.0f], |
||
88 | [NSNumber numberWithFloat:1.0f], |
||
89 | [NSNumber numberWithFloat:0.483f], |
||
90 | nil]; |
||
91 | |||
92 | NSMutableArray *colors2 = [NSMutableArray arrayWithCapacity:4]; |
||
93 | color = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1.0]; |
||
94 | [colors2 addObject:(id)[color CGColor]]; |
||
95 | color = [UIColor colorWithRed:0.656 green:0.683 blue:0.713 alpha:1.0]; |
||
96 | [colors2 addObject:(id)[color CGColor]]; |
||
97 | color = [UIColor colorWithRed:0.137 green:0.155 blue:0.208 alpha:1.0]; |
||
98 | [colors2 addObject:(id)[color CGColor]]; |
||
99 | color = [UIColor colorWithRed:0.237 green:0.257 blue:0.305 alpha:1.0]; |
||
100 | [colors2 addObject:(id)[color CGColor]]; |
||
101 | self.highlightGradientColors = colors2; |
||
102 | self.highlightGradientLocations = [NSArray arrayWithObjects: |
||
103 | [NSNumber numberWithFloat:0.0f], |
||
104 | [NSNumber numberWithFloat:1.0f], |
||
105 | [NSNumber numberWithFloat:0.51f], |
||
106 | [NSNumber numberWithFloat:0.654f], |
||
107 | nil]; |
||
108 | self.cornerRadius = 7.0f; |
||
109 | [self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; |
||
110 | [self setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted]; |
||
111 | |||
112 | } |
||
113 | - (void)useRedDeleteStyle |
||
114 | { |
||
115 | NSMutableArray *colors = [NSMutableArray arrayWithCapacity:5]; |
||
116 | UIColor *color = [UIColor colorWithRed:0.667 green:0.15 blue:0.152 alpha:1.0]; |
||
117 | [colors addObject:(id)[color CGColor]]; |
||
118 | color = [UIColor colorWithRed:0.841 green:0.566 blue:0.566 alpha:1.0]; |
||
119 | [colors addObject:(id)[color CGColor]]; |
||
120 | color = [UIColor colorWithRed:0.75 green:0.341 blue:0.345 alpha:1.0]; |
||
121 | [colors addObject:(id)[color CGColor]]; |
||
122 | color = [UIColor colorWithRed:0.592 green:0.0 blue:0.0 alpha:1.0]; |
||
123 | [colors addObject:(id)[color CGColor]]; |
||
124 | color = [UIColor colorWithRed:0.592 green:0.0 blue:0.0 alpha:1.0]; |
||
125 | [colors addObject:(id)[color CGColor]]; |
||
126 | self.normalGradientColors = colors; |
||
127 | self.normalGradientLocations = [NSMutableArray arrayWithObjects: |
||
128 | [NSNumber numberWithFloat:0.0f], |
||
129 | [NSNumber numberWithFloat:1.0f], |
||
130 | [NSNumber numberWithFloat:0.582f], |
||
131 | [NSNumber numberWithFloat:0.418f], |
||
132 | [NSNumber numberWithFloat:0.346], |
||
133 | nil]; |
||
134 | |||
135 | NSMutableArray *colors2 = [NSMutableArray arrayWithCapacity:5]; |
||
136 | color = [UIColor colorWithRed:0.467 green:0.009 blue:0.005 alpha:1.0]; |
||
137 | [colors2 addObject:(id)[color CGColor]]; |
||
138 | color = [UIColor colorWithRed:0.754 green:0.562 blue:0.562 alpha:1.0]; |
||
139 | [colors2 addObject:(id)[color CGColor]]; |
||
140 | color = [UIColor colorWithRed:0.543 green:0.212 blue:0.212 alpha:1.0]; |
||
141 | [colors2 addObject:(id)[color CGColor]]; |
||
142 | color = [UIColor colorWithRed:0.5 green:0.153 blue:0.152 alpha:1.0]; |
||
143 | [colors2 addObject:(id)[color CGColor]]; |
||
144 | color = [UIColor colorWithRed:0.388 green:0.004 blue:0.0 alpha:1.0]; |
||
145 | [colors addObject:(id)[color CGColor]]; |
||
146 | |||
147 | self.highlightGradientColors = colors; |
||
148 | self.highlightGradientLocations = [NSMutableArray arrayWithObjects: |
||
149 | [NSNumber numberWithFloat:0.0f], |
||
150 | [NSNumber numberWithFloat:1.0f], |
||
151 | [NSNumber numberWithFloat:0.715f], |
||
152 | [NSNumber numberWithFloat:0.513f], |
||
153 | [NSNumber numberWithFloat:0.445f], |
||
154 | nil]; |
||
155 | self.cornerRadius = 9.f; |
||
156 | [self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; |
||
157 | |||
158 | } |
||
159 | - (void)useWhiteStyle |
||
160 | { |
||
161 | NSMutableArray *colors = [NSMutableArray arrayWithCapacity:3]; |
||
162 | UIColor *color = [UIColor colorWithRed:0.864 green:0.864 blue:0.864 alpha:1.0]; |
||
163 | [colors addObject:(id)[color CGColor]]; |
||
164 | color = [UIColor colorWithRed:0.995 green:0.995 blue:0.995 alpha:1.0]; |
||
165 | [colors addObject:(id)[color CGColor]]; |
||
166 | color = [UIColor colorWithRed:0.956 green:0.956 blue:0.955 alpha:1.0]; |
||
167 | [colors addObject:(id)[color CGColor]]; |
||
168 | self.normalGradientColors = colors; |
||
169 | self.normalGradientLocations = [NSMutableArray arrayWithObjects: |
||
170 | [NSNumber numberWithFloat:0.0f], |
||
171 | [NSNumber numberWithFloat:1.0f], |
||
172 | [NSNumber numberWithFloat:0.601f], |
||
173 | nil]; |
||
174 | |||
175 | NSMutableArray *colors2 = [NSMutableArray arrayWithCapacity:3]; |
||
176 | color = [UIColor colorWithRed:0.692 green:0.692 blue:0.691 alpha:1.0]; |
||
177 | [colors2 addObject:(id)[color CGColor]]; |
||
178 | color = [UIColor colorWithRed:0.995 green:0.995 blue:0.995 alpha:1.0]; |
||
179 | [colors2 addObject:(id)[color CGColor]]; |
||
180 | color = [UIColor colorWithRed:0.83 green:0.83 blue:0.83 alpha:1.0]; |
||
181 | [colors2 addObject:(id)[color CGColor]]; |
||
182 | self.highlightGradientColors = colors2; |
||
183 | self.highlightGradientLocations = [NSMutableArray arrayWithObjects: |
||
184 | [NSNumber numberWithFloat:0.0f], |
||
185 | [NSNumber numberWithFloat:1.0f], |
||
186 | [NSNumber numberWithFloat:0.601f], |
||
187 | nil]; |
||
188 | |||
189 | self.cornerRadius = 9.f; |
||
190 | [self setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; |
||
191 | [self setTitleColor:[UIColor darkGrayColor] forState:UIControlStateHighlighted]; |
||
192 | } |
||
193 | - (void)useBlackStyle |
||
194 | { |
||
195 | NSMutableArray *colors = [NSMutableArray arrayWithCapacity:4]; |
||
196 | UIColor *color = [UIColor colorWithRed:0.154 green:0.154 blue:0.154 alpha:1.0]; |
||
197 | [colors addObject:(id)[color CGColor]]; |
||
198 | color = [UIColor colorWithRed:0.307 green:0.307 blue:0.307 alpha:1.0]; |
||
199 | [colors addObject:(id)[color CGColor]];; |
||
200 | color = [UIColor colorWithRed:0.166 green:0.166 blue:0.166 alpha:1.0]; |
||
201 | [colors addObject:(id)[color CGColor]]; |
||
202 | color = [UIColor colorWithRed:0.118 green:0.118 blue:0.118 alpha:1.0]; |
||
203 | [colors addObject:(id)[color CGColor]]; |
||
204 | self.normalGradientColors = colors; |
||
205 | self.normalGradientLocations = [NSMutableArray arrayWithObjects: |
||
206 | [NSNumber numberWithFloat:0.0f], |
||
207 | [NSNumber numberWithFloat:1.0f], |
||
208 | [NSNumber numberWithFloat:0.548f], |
||
209 | [NSNumber numberWithFloat:0.462f], |
||
210 | nil]; |
||
211 | self.cornerRadius = 9.0f; |
||
212 | |||
213 | NSMutableArray *colors2 = [NSMutableArray arrayWithCapacity:4]; |
||
214 | color = [UIColor colorWithRed:0.199 green:0.199 blue:0.199 alpha:1.0]; |
||
215 | [colors2 addObject:(id)[color CGColor]]; |
||
216 | color = [UIColor colorWithRed:0.04 green:0.04 blue:0.04 alpha:1.0]; |
||
217 | [colors2 addObject:(id)[color CGColor]]; |
||
218 | color = [UIColor colorWithRed:0.074 green:0.074 blue:0.074 alpha:1.0]; |
||
219 | [colors2 addObject:(id)[color CGColor]]; |
||
220 | color = [UIColor colorWithRed:0.112 green:0.112 blue:0.112 alpha:1.0]; |
||
221 | [colors2 addObject:(id)[color CGColor]]; |
||
222 | |||
223 | self.highlightGradientColors = colors2; |
||
224 | self.highlightGradientLocations = [NSMutableArray arrayWithObjects: |
||
225 | [NSNumber numberWithFloat:0.0f], |
||
226 | [NSNumber numberWithFloat:1.0f], |
||
227 | [NSNumber numberWithFloat:0.548f], |
||
228 | [NSNumber numberWithFloat:0.462f], |
||
229 | nil]; |
||
230 | [self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; |
||
231 | } |
||
232 | - (void)useSimpleOrangeStyle |
||
233 | { |
||
234 | NSMutableArray *colors = [NSMutableArray arrayWithCapacity:2]; |
||
235 | UIColor *color = [UIColor colorWithRed:0.935 green:0.403 blue:0.02 alpha:1.0]; |
||
236 | [colors addObject:(id)[color CGColor]]; |
||
237 | color = [UIColor colorWithRed:0.97 green:0.582 blue:0.0 alpha:1.0]; |
||
238 | [colors addObject:(id)[color CGColor]]; |
||
239 | self.normalGradientColors = colors; |
||
240 | self.normalGradientLocations = [NSMutableArray arrayWithObjects: |
||
241 | [NSNumber numberWithFloat:0.0f], |
||
242 | [NSNumber numberWithFloat:1.0f], |
||
243 | nil]; |
||
244 | |||
245 | NSMutableArray *colors2 = [NSMutableArray arrayWithCapacity:3]; |
||
246 | color = [UIColor colorWithRed:0.914 green:0.309 blue:0.0 alpha:1.0]; |
||
247 | [colors2 addObject:(id)[color CGColor]]; |
||
248 | color = [UIColor colorWithRed:0.935 green:0.4 blue:0.0 alpha:1.0]; |
||
249 | [colors2 addObject:(id)[color CGColor]]; |
||
250 | color = [UIColor colorWithRed:0.946 green:0.441 blue:0.01 alpha:1.0]; |
||
251 | [colors2 addObject:(id)[color CGColor]]; |
||
252 | self.highlightGradientColors = colors2; |
||
253 | self.highlightGradientLocations = [NSMutableArray arrayWithObjects: |
||
254 | [NSNumber numberWithFloat:0.0f], |
||
255 | [NSNumber numberWithFloat:1.0f], |
||
256 | [NSNumber numberWithFloat:0.498f], |
||
257 | nil]; |
||
258 | |||
259 | self.cornerRadius = 9.f; |
||
260 | [self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; |
||
261 | } |
||
262 | #pragma mark - |
||
263 | - (void)drawRect:(CGRect)rect |
||
264 | { |
||
265 | self.backgroundColor = [UIColor clearColor]; |
||
266 | CGRect imageBounds = CGRectMake(0.0, 0.0, self.bounds.size.width - 0.5, self.bounds.size.height); |
||
267 | CGFloat alignStroke; |
||
268 | CGFloat resolution; |
||
269 | CGFloat stroke; |
||
270 | CGMutablePathRef path; |
||
271 | CGPoint point; |
||
272 | CGPoint controlPoint1; |
||
273 | CGPoint controlPoint2; |
||
274 | CGGradientRef gradient; |
||
275 | UIColor *color; |
||
276 | CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB(); |
||
277 | CGContextRef context = UIGraphicsGetCurrentContext(); |
||
278 | CGPoint point2; |
||
279 | |||
280 | resolution = 0.5 * (self.bounds.size.width / imageBounds.size.width + self.bounds.size.height / imageBounds.size.height); |
||
281 | |||
282 | stroke = 1.0; |
||
283 | stroke *= resolution; |
||
284 | if (stroke < 1.0) |
||
285 | stroke = ceil(stroke); |
||
286 | else |
||
287 | stroke = round(stroke); |
||
288 | stroke /= resolution; |
||
289 | alignStroke = fmod(0.5 * stroke * resolution, 1.0); |
||
290 | path = CGPathCreateMutable(); |
||
291 | point = CGPointMake((self.bounds.size.width - [self cornerRadius]), self.bounds.size.height - 0.5f); |
||
292 | point.x = (round(resolution * point.x + alignStroke) - alignStroke) / resolution; |
||
293 | point.y = (round(resolution * point.y + alignStroke) - alignStroke) / resolution; |
||
294 | CGPathMoveToPoint(path, NULL, point.x, point.y); |
||
295 | point = CGPointMake(self.bounds.size.width - 0.5f, (self.bounds.size.height - [self cornerRadius])); |
||
296 | point.x = (round(resolution * point.x + alignStroke) - alignStroke) / resolution; |
||
297 | point.y = (round(resolution * point.y + alignStroke) - alignStroke) / resolution; |
||
298 | controlPoint1 = CGPointMake((self.bounds.size.width - ([self cornerRadius] / 2.f)), self.bounds.size.height - 0.5f); |
||
299 | controlPoint1.x = (round(resolution * controlPoint1.x + alignStroke) - alignStroke) / resolution; |
||
300 | controlPoint1.y = (round(resolution * controlPoint1.y + alignStroke) - alignStroke) / resolution; |
||
301 | controlPoint2 = CGPointMake(self.bounds.size.width - 0.5f, (self.bounds.size.height - ([self cornerRadius] / 2.f))); |
||
302 | controlPoint2.x = (round(resolution * controlPoint2.x + alignStroke) - alignStroke) / resolution; |
||
303 | controlPoint2.y = (round(resolution * controlPoint2.y + alignStroke) - alignStroke) / resolution; |
||
304 | CGPathAddCurveToPoint(path, NULL, controlPoint1.x, controlPoint1.y, controlPoint2.x, controlPoint2.y, point.x, point.y); |
||
305 | point = CGPointMake(self.bounds.size.width - 0.5f, [self cornerRadius]); |
||
306 | point.x = (round(resolution * point.x + alignStroke) - alignStroke) / resolution; |
||
307 | point.y = (round(resolution * point.y + alignStroke) - alignStroke) / resolution; |
||
308 | CGPathAddLineToPoint(path, NULL, point.x, point.y); |
||
309 | point = CGPointMake((self.bounds.size.width - [self cornerRadius]), 0.0); |
||
310 | point.x = (round(resolution * point.x + alignStroke) - alignStroke) / resolution; |
||
311 | point.y = (round(resolution * point.y + alignStroke) - alignStroke) / resolution; |
||
312 | controlPoint1 = CGPointMake(self.bounds.size.width - 0.5f, ([self cornerRadius] / 2.f)); |
||
313 | controlPoint1.x = (round(resolution * controlPoint1.x + alignStroke) - alignStroke) / resolution; |
||
314 | controlPoint1.y = (round(resolution * controlPoint1.y + alignStroke) - alignStroke) / resolution; |
||
315 | controlPoint2 = CGPointMake((self.bounds.size.width - ([self cornerRadius] / 2.f)), 0.0); |
||
316 | controlPoint2.x = (round(resolution * controlPoint2.x + alignStroke) - alignStroke) / resolution; |
||
317 | controlPoint2.y = (round(resolution * controlPoint2.y + alignStroke) - alignStroke) / resolution; |
||
318 | CGPathAddCurveToPoint(path, NULL, controlPoint1.x, controlPoint1.y, controlPoint2.x, controlPoint2.y, point.x, point.y); |
||
319 | point = CGPointMake([self cornerRadius], 0.0); |
||
320 | point.x = (round(resolution * point.x + alignStroke) - alignStroke) / resolution; |
||
321 | point.y = (round(resolution * point.y + alignStroke) - alignStroke) / resolution; |
||
322 | CGPathAddLineToPoint(path, NULL, point.x, point.y); |
||
323 | point = CGPointMake(0.0, [self cornerRadius]); |
||
324 | point.x = (round(resolution * point.x + alignStroke) - alignStroke) / resolution; |
||
325 | point.y = (round(resolution * point.y + alignStroke) - alignStroke) / resolution; |
||
326 | controlPoint1 = CGPointMake(([self cornerRadius] / 2.f), 0.0); |
||
327 | controlPoint1.x = (round(resolution * controlPoint1.x + alignStroke) - alignStroke) / resolution; |
||
328 | controlPoint1.y = (round(resolution * controlPoint1.y + alignStroke) - alignStroke) / resolution; |
||
329 | controlPoint2 = CGPointMake(0.0, ([self cornerRadius] / 2.f)); |
||
330 | controlPoint2.x = (round(resolution * controlPoint2.x + alignStroke) - alignStroke) / resolution; |
||
331 | controlPoint2.y = (round(resolution * controlPoint2.y + alignStroke) - alignStroke) / resolution; |
||
332 | CGPathAddCurveToPoint(path, NULL, controlPoint1.x, controlPoint1.y, controlPoint2.x, controlPoint2.y, point.x, point.y); |
||
333 | point = CGPointMake(0.0, (self.bounds.size.height - [self cornerRadius])); |
||
334 | point.x = (round(resolution * point.x + alignStroke) - alignStroke) / resolution; |
||
335 | point.y = (round(resolution * point.y + alignStroke) - alignStroke) / resolution; |
||
336 | CGPathAddLineToPoint(path, NULL, point.x, point.y); |
||
337 | point = CGPointMake([self cornerRadius], self.bounds.size.height - 0.5f); |
||
338 | point.x = (round(resolution * point.x + alignStroke) - alignStroke) / resolution; |
||
339 | point.y = (round(resolution * point.y + alignStroke) - alignStroke) / resolution; |
||
340 | controlPoint1 = CGPointMake(0.0, (self.bounds.size.height - ([self cornerRadius] / 2.f))); |
||
341 | controlPoint1.x = (round(resolution * controlPoint1.x + alignStroke) - alignStroke) / resolution; |
||
342 | controlPoint1.y = (round(resolution * controlPoint1.y + alignStroke) - alignStroke) / resolution; |
||
343 | controlPoint2 = CGPointMake(([self cornerRadius] / 2.f), self.bounds.size.height - 0.5f); |
||
344 | controlPoint2.x = (round(resolution * controlPoint2.x + alignStroke) - alignStroke) / resolution; |
||
345 | controlPoint2.y = (round(resolution * controlPoint2.y + alignStroke) - alignStroke) / resolution; |
||
346 | CGPathAddCurveToPoint(path, NULL, controlPoint1.x, controlPoint1.y, controlPoint2.x, controlPoint2.y, point.x, point.y); |
||
347 | point = CGPointMake((self.bounds.size.width - [self cornerRadius]), self.bounds.size.height - 0.5f); |
||
348 | point.x = (round(resolution * point.x + alignStroke) - alignStroke) / resolution; |
||
349 | point.y = (round(resolution * point.y + alignStroke) - alignStroke) / resolution; |
||
350 | CGPathAddLineToPoint(path, NULL, point.x, point.y); |
||
351 | CGPathCloseSubpath(path); |
||
352 | if (self.state == UIControlStateHighlighted) |
||
353 | gradient = self.highlightGradient; |
||
354 | else |
||
355 | gradient = self.normalGradient; |
||
356 | |||
357 | CGContextAddPath(context, path); |
||
358 | CGContextSaveGState(context); |
||
359 | CGContextEOClip(context); |
||
360 | point = CGPointMake((self.bounds.size.width / 2.0), self.bounds.size.height - 0.5f); |
||
361 | point2 = CGPointMake((self.bounds.size.width / 2.0), 0.0); |
||
362 | CGContextDrawLinearGradient(context, gradient, point, point2, (kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation)); |
||
363 | CGContextRestoreGState(context); |
||
364 | color = [UIColor colorWithRed:0.076 green:0.103 blue:0.195 alpha:1.0]; |
||
365 | [color setStroke]; |
||
366 | CGContextSetLineWidth(context, stroke); |
||
367 | CGContextSetLineCap(context, kCGLineCapSquare); |
||
368 | CGContextAddPath(context, path); |
||
369 | CGContextStrokePath(context); |
||
370 | CGPathRelease(path); |
||
371 | |||
372 | CGColorSpaceRelease(space); |
||
373 | } |
||
374 | #pragma mark - |
||
375 | #pragma mark Touch Handling |
||
376 | - (void)hesitateUpdate |
||
377 | { |
||
378 | [self setNeedsDisplay]; |
||
379 | } |
||
380 | - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event |
||
381 | { |
||
382 | [super touchesBegan:touches withEvent:event]; |
||
383 | [self setNeedsDisplay]; |
||
384 | } |
||
385 | - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event |
||
386 | { |
||
387 | [super touchesCancelled:touches withEvent:event]; |
||
388 | [self setNeedsDisplay]; |
||
389 | [self performSelector:@selector(hesitateUpdate) withObject:nil afterDelay:0.1]; |
||
390 | } |
||
391 | - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event |
||
392 | { |
||
393 | [super touchesMoved:touches withEvent:event]; |
||
394 | [self setNeedsDisplay]; |
||
395 | |||
396 | } |
||
397 | - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event |
||
398 | { |
||
399 | [super touchesEnded:touches withEvent:event]; |
||
400 | [self setNeedsDisplay]; |
||
401 | [self performSelector:@selector(hesitateUpdate) withObject:nil afterDelay:0.1]; |
||
402 | } |
||
403 | #pragma mark - |
||
404 | #pragma mark NSCoding |
||
405 | - (void)encodeWithCoder:(NSCoder *)encoder |
||
406 | { |
||
407 | [super encodeWithCoder:encoder]; |
||
408 | [encoder encodeObject:[self normalGradientColors] forKey:@"normalGradientColors"]; |
||
409 | [encoder encodeObject:[self normalGradientLocations] forKey:@"normalGradientLocations"]; |
||
410 | [encoder encodeObject:[self highlightGradientColors] forKey:@"highlightGradientColors"]; |
||
411 | [encoder encodeObject:[self highlightGradientLocations] forKey:@"highlightGradientLocations"]; |
||
412 | } |
||
413 | |||
414 | - (id)initWithCoder:(NSCoder *)decoder |
||
415 | { |
||
416 | if (self = [super initWithCoder:decoder]) |
||
417 | { |
||
418 | [self setNormalGradientColors:[decoder decodeObjectForKey:@"normalGradientColors"]]; |
||
419 | [self setNormalGradientLocations:[decoder decodeObjectForKey:@"normalGradientLocations"]]; |
||
420 | [self setHighlightGradientColors:[decoder decodeObjectForKey:@"highlightGradientColors"]]; |
||
421 | [self setHighlightGradientLocations:[decoder decodeObjectForKey:@"highlightGradientLocations"]]; |
||
422 | |||
423 | if (self.normalGradientColors == nil) |
||
424 | [self useWhiteStyle]; |
||
425 | |||
426 | [self setOpaque:NO]; |
||
427 | self.backgroundColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.0]; |
||
428 | } |
||
429 | return self; |
||
430 | } |
||
431 | #pragma mark - |
||
432 | - (void)dealloc |
||
433 | { |
||
434 | [normalGradientColors release]; |
||
435 | [normalGradientLocations release]; |
||
436 | [highlightGradientColors release]; |
||
437 | [highlightGradientLocations release]; |
||
438 | |||
439 | if (normalGradient != NULL) |
||
440 | CGGradientRelease(normalGradient); |
||
441 | if (highlightGradient != NULL) |
||
442 | CGGradientRelease(highlightGradient); |
||
443 | [super dealloc]; |
||
444 | } |
||
445 | |||
446 | @end |