Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 584 lines (546 sloc) 29.28 kB
8a6043f @steipete initial commit
authored
1 //
2 // ButtonGradientView.m
3 // Custom Alert View
4 //
5 // Created by jeff on 5/17/10.
6 // Copyright 2010 Peter Steinberger. 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 strokeWeight, strokeColor;
25 @synthesize normalGradient, highlightGradient;
26 #pragma mark -
27 - (CGGradientRef)normalGradient
28 {
29 if (normalGradient == NULL)
30 {
31 int locCount = [normalGradientLocations count];
32 CGFloat locations[locCount];
33 for (int i = 0; i < [normalGradientLocations count]; i++)
34 {
35 NSNumber *location = [normalGradientLocations objectAtIndex:i];
36 locations[i] = [location floatValue];
37 }
38 CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
39
40 normalGradient = CGGradientCreateWithColors(space, (CFArrayRef)normalGradientColors, locations);
41 CGColorSpaceRelease(space);
42 }
43 return normalGradient;
44 }
45 - (CGGradientRef)highlightGradient
46 {
47
48 if (highlightGradient == NULL)
49 {
50 CGFloat locations[[highlightGradientLocations count]];
51 for (int i = 0; i < [highlightGradientLocations count]; i++)
52 {
53 NSNumber *location = [highlightGradientLocations objectAtIndex:i];
54 locations[i] = [location floatValue];
55 }
56 CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
57
58 highlightGradient = CGGradientCreateWithColors(space, (CFArrayRef)highlightGradientColors, locations);
59 CGColorSpaceRelease(space);
60 }
61 return highlightGradient;
62 }
63 #pragma mark -
64 - (id)initWithFrame:(CGRect)frame
65 {
66 self = [super initWithFrame:frame];
67 if (self)
68 {
69 [self setOpaque:NO];
70 self.backgroundColor = [UIColor clearColor];
71 }
72 return self;
73 }
74 #pragma mark -
75 #pragma mark Appearances
76 - (void)useAlertStyle
77 {
78 // Oddly enough, if I create the color array using arrayWithObjects:, it
79 // doesn't work - the gradient comes back NULL
80 NSMutableArray *colors = [NSMutableArray arrayWithCapacity:3];
81 UIColor *color = [UIColor colorWithRed:0.283 green:0.32 blue:0.414 alpha:1.0];
82 [colors addObject:(id)[color CGColor]];
83 color = [UIColor colorWithRed:0.82 green:0.834 blue:0.87 alpha:1.0];
84 [colors addObject:(id)[color CGColor]];
85 color = [UIColor colorWithRed:0.186 green:0.223 blue:0.326 alpha:1.0];
86 [colors addObject:(id)[color CGColor]];
87 self.normalGradientColors = colors;
88 self.normalGradientLocations = [NSArray arrayWithObjects:
89 [NSNumber numberWithFloat:0.0f],
90 [NSNumber numberWithFloat:1.0f],
91 [NSNumber numberWithFloat:0.483f],
92 nil];
93
94 NSMutableArray *colors2 = [NSMutableArray arrayWithCapacity:4];
95 color = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1.0];
96 [colors2 addObject:(id)[color CGColor]];
97 color = [UIColor colorWithRed:0.656 green:0.683 blue:0.713 alpha:1.0];
98 [colors2 addObject:(id)[color CGColor]];
99 color = [UIColor colorWithRed:0.137 green:0.155 blue:0.208 alpha:1.0];
100 [colors2 addObject:(id)[color CGColor]];
101 color = [UIColor colorWithRed:0.237 green:0.257 blue:0.305 alpha:1.0];
102 [colors2 addObject:(id)[color CGColor]];
103 self.highlightGradientColors = colors2;
104 self.highlightGradientLocations = [NSArray arrayWithObjects:
105 [NSNumber numberWithFloat:0.0f],
106 [NSNumber numberWithFloat:1.0f],
107 [NSNumber numberWithFloat:0.51f],
108 [NSNumber numberWithFloat:0.654f],
109 nil];
110 self.cornerRadius = 7.0f;
111 [self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
112 [self setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
113
114 }
115 - (void)useRedDeleteStyle
116 {
117 NSMutableArray *colors = [NSMutableArray arrayWithCapacity:5];
118 UIColor *color = [UIColor colorWithRed:0.667 green:0.15 blue:0.152 alpha:1.0];
119 [colors addObject:(id)[color CGColor]];
120 color = [UIColor colorWithRed:0.841 green:0.566 blue:0.566 alpha:1.0];
121 [colors addObject:(id)[color CGColor]];
122 color = [UIColor colorWithRed:0.75 green:0.341 blue:0.345 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 color = [UIColor colorWithRed:0.592 green:0.0 blue:0.0 alpha:1.0];
127 [colors addObject:(id)[color CGColor]];
128 self.normalGradientColors = colors;
129 self.normalGradientLocations = [NSArray arrayWithObjects:
130 [NSNumber numberWithFloat:0.0f],
131 [NSNumber numberWithFloat:1.0f],
132 [NSNumber numberWithFloat:0.582f],
133 [NSNumber numberWithFloat:0.418f],
134 [NSNumber numberWithFloat:0.346],
135 nil];
136
137 NSMutableArray *colors2 = [NSMutableArray arrayWithCapacity:5];
138 color = [UIColor colorWithRed:0.467 green:0.009 blue:0.005 alpha:1.0];
139 [colors2 addObject:(id)[color CGColor]];
140 color = [UIColor colorWithRed:0.754 green:0.562 blue:0.562 alpha:1.0];
141 [colors2 addObject:(id)[color CGColor]];
142 color = [UIColor colorWithRed:0.543 green:0.212 blue:0.212 alpha:1.0];
143 [colors2 addObject:(id)[color CGColor]];
144 color = [UIColor colorWithRed:0.5 green:0.153 blue:0.152 alpha:1.0];
145 [colors2 addObject:(id)[color CGColor]];
146 color = [UIColor colorWithRed:0.388 green:0.004 blue:0.0 alpha:1.0];
147 [colors addObject:(id)[color CGColor]];
148
149 self.highlightGradientColors = colors;
150 self.highlightGradientLocations = [NSArray arrayWithObjects:
151 [NSNumber numberWithFloat:0.0f],
152 [NSNumber numberWithFloat:1.0f],
153 [NSNumber numberWithFloat:0.715f],
154 [NSNumber numberWithFloat:0.513f],
155 [NSNumber numberWithFloat:0.445f],
156 nil];
157 self.cornerRadius = 9.f;
158 [self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
159
160 }
161 - (void)useWhiteStyle
162 {
163 NSMutableArray *colors = [NSMutableArray arrayWithCapacity:3];
164 UIColor *color = [UIColor colorWithRed:0.864 green:0.864 blue:0.864 alpha:1.0];
165 [colors addObject:(id)[color CGColor]];
166 color = [UIColor colorWithRed:0.995 green:0.995 blue:0.995 alpha:1.0];
167 [colors addObject:(id)[color CGColor]];
168 color = [UIColor colorWithRed:0.956 green:0.956 blue:0.955 alpha:1.0];
169 [colors addObject:(id)[color CGColor]];
170 self.normalGradientColors = colors;
171 self.normalGradientLocations = [NSMutableArray arrayWithObjects:
172 [NSNumber numberWithFloat:0.0f],
173 [NSNumber numberWithFloat:1.0f],
174 [NSNumber numberWithFloat:0.601f],
175 nil];
176
177 NSMutableArray *colors2 = [NSMutableArray arrayWithCapacity:3];
178 color = [UIColor colorWithRed:0.692 green:0.692 blue:0.691 alpha:1.0];
179 [colors2 addObject:(id)[color CGColor]];
180 color = [UIColor colorWithRed:0.995 green:0.995 blue:0.995 alpha:1.0];
181 [colors2 addObject:(id)[color CGColor]];
182 color = [UIColor colorWithRed:0.83 green:0.83 blue:0.83 alpha:1.0];
183 [colors2 addObject:(id)[color CGColor]];
184 self.highlightGradientColors = colors2;
185 self.highlightGradientLocations = [NSMutableArray arrayWithObjects:
186 [NSNumber numberWithFloat:0.0f],
187 [NSNumber numberWithFloat:1.0f],
188 [NSNumber numberWithFloat:0.601f],
189 nil];
190
191 self.cornerRadius = 9.f;
192 [self setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
193 [self setTitleColor:[UIColor darkGrayColor] forState:UIControlStateHighlighted];
194 }
195 - (void)useBlackStyle
196 {
197 NSMutableArray *colors = [NSMutableArray arrayWithCapacity:4];
198 UIColor *color = [UIColor colorWithRed:0.154 green:0.154 blue:0.154 alpha:1.0];
199 [colors addObject:(id)[color CGColor]];
200 color = [UIColor colorWithRed:0.307 green:0.307 blue:0.307 alpha:1.0];
201 [colors addObject:(id)[color CGColor]];;
202 color = [UIColor colorWithRed:0.166 green:0.166 blue:0.166 alpha:1.0];
203 [colors addObject:(id)[color CGColor]];
204 color = [UIColor colorWithRed:0.118 green:0.118 blue:0.118 alpha:1.0];
205 [colors addObject:(id)[color CGColor]];
206 self.normalGradientColors = colors;
207 self.normalGradientLocations = [NSMutableArray arrayWithObjects:
208 [NSNumber numberWithFloat:0.0f],
209 [NSNumber numberWithFloat:1.0f],
210 [NSNumber numberWithFloat:0.548f],
211 [NSNumber numberWithFloat:0.462f],
212 nil];
213 self.cornerRadius = 9.0f;
214
215 NSMutableArray *colors2 = [NSMutableArray arrayWithCapacity:4];
216 color = [UIColor colorWithRed:0.199 green:0.199 blue:0.199 alpha:1.0];
217 [colors2 addObject:(id)[color CGColor]];
218 color = [UIColor colorWithRed:0.04 green:0.04 blue:0.04 alpha:1.0];
219 [colors2 addObject:(id)[color CGColor]];
220 color = [UIColor colorWithRed:0.074 green:0.074 blue:0.074 alpha:1.0];
221 [colors2 addObject:(id)[color CGColor]];
222 color = [UIColor colorWithRed:0.112 green:0.112 blue:0.112 alpha:1.0];
223 [colors2 addObject:(id)[color CGColor]];
224
225 self.highlightGradientColors = colors2;
226 self.highlightGradientLocations = [NSMutableArray arrayWithObjects:
227 [NSNumber numberWithFloat:0.0f],
228 [NSNumber numberWithFloat:1.0f],
229 [NSNumber numberWithFloat:0.548f],
230 [NSNumber numberWithFloat:0.462f],
231 nil];
232 [self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
233 }
234 - (void)useWhiteActionSheetStyle
235 {
236 NSMutableArray *colors = [NSMutableArray arrayWithCapacity:3];
237 UIColor *color = [UIColor colorWithRed:0.864 green:0.864 blue:0.864 alpha:1.0];
238 [colors addObject:(id)[color CGColor]];
239 color = [UIColor colorWithRed:0.995 green:0.995 blue:0.995 alpha:1.0];
240 [colors addObject:(id)[color CGColor]];
241 color = [UIColor colorWithRed:0.956 green:0.956 blue:0.955 alpha:1.0];
242 [colors addObject:(id)[color CGColor]];
243 self.normalGradientColors = colors;
244 self.normalGradientLocations = [NSMutableArray arrayWithObjects:
245 [NSNumber numberWithFloat:0.0f],
246 [NSNumber numberWithFloat:1.0f],
247 [NSNumber numberWithFloat:0.601f],
248 nil];
249
250 NSMutableArray *colors2 = [NSMutableArray arrayWithCapacity:7];
251 color = [UIColor colorWithRed:0.033 green:0.251 blue:0.673 alpha:1.0];
252 [colors2 addObject:(id)[color CGColor]];
253 color = [UIColor colorWithRed:0.66 green:0.701 blue:0.88 alpha:1.0];
254 [colors2 addObject:(id)[color CGColor]];
255 color = [UIColor colorWithRed:0.222 green:0.308 blue:0.709 alpha:1.0];
256 [colors2 addObject:(id)[color CGColor]];
257 color = [UIColor colorWithRed:0.145 green:0.231 blue:0.683 alpha:1.0];
258 [colors2 addObject:(id)[color CGColor]];
259 color = [UIColor colorWithRed:0.0 green:0.124 blue:0.621 alpha:1.0];
260 [colors2 addObject:(id)[color CGColor]];
261 color = [UIColor colorWithRed:0.011 green:0.181 blue:0.647 alpha:1.0];
262 [colors2 addObject:(id)[color CGColor]];
263 color = [UIColor colorWithRed:0.311 green:0.383 blue:0.748 alpha:1.0];
264 [colors2 addObject:(id)[color CGColor]];
265 self.highlightGradientColors = colors2;
266 self.highlightGradientLocations = [NSMutableArray arrayWithObjects:
267 [NSNumber numberWithFloat:0.0f],
268 [NSNumber numberWithFloat:1.0f],
269 [NSNumber numberWithFloat:0.957f],
270 [NSNumber numberWithFloat:0.574f],
271 [NSNumber numberWithFloat:0.541],
272 [NSNumber numberWithFloat:0.185f],
273 [NSNumber numberWithFloat:0.812f],
274 nil];
275
276 self.cornerRadius = 9.f;
277 [self setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
278 [self setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
279 }
280 - (void)useBlackActionSheetStyle
281 {
282 NSMutableArray *colors = [NSMutableArray arrayWithCapacity:4];
283 UIColor *color = [UIColor colorWithRed:0.154 green:0.154 blue:0.154 alpha:1.0];
284 [colors addObject:(id)[color CGColor]];
285 color = [UIColor colorWithRed:0.307 green:0.307 blue:0.307 alpha:1.0];
286 [colors addObject:(id)[color CGColor]];;
287 color = [UIColor colorWithRed:0.166 green:0.166 blue:0.166 alpha:1.0];
288 [colors addObject:(id)[color CGColor]];
289 color = [UIColor colorWithRed:0.118 green:0.118 blue:0.118 alpha:1.0];
290 [colors addObject:(id)[color CGColor]];
291 self.normalGradientColors = colors;
292 self.normalGradientLocations = [NSMutableArray arrayWithObjects:
293 [NSNumber numberWithFloat:0.0f],
294 [NSNumber numberWithFloat:1.0f],
295 [NSNumber numberWithFloat:0.548f],
296 [NSNumber numberWithFloat:0.462f],
297 nil];
298 self.cornerRadius = 9.0f;
299
300 NSMutableArray *colors2 = [NSMutableArray arrayWithCapacity:7];
301 color = [UIColor colorWithRed:0.033 green:0.251 blue:0.673 alpha:1.0];
302 [colors2 addObject:(id)[color CGColor]];
303 color = [UIColor colorWithRed:0.66 green:0.701 blue:0.88 alpha:1.0];
304 [colors2 addObject:(id)[color CGColor]];
305 color = [UIColor colorWithRed:0.222 green:0.308 blue:0.709 alpha:1.0];
306 [colors2 addObject:(id)[color CGColor]];
307 color = [UIColor colorWithRed:0.145 green:0.231 blue:0.683 alpha:1.0];
308 [colors2 addObject:(id)[color CGColor]];
309 color = [UIColor colorWithRed:0.0 green:0.124 blue:0.621 alpha:1.0];
310 [colors2 addObject:(id)[color CGColor]];
311 color = [UIColor colorWithRed:0.011 green:0.181 blue:0.647 alpha:1.0];
312 [colors2 addObject:(id)[color CGColor]];
313 color = [UIColor colorWithRed:0.311 green:0.383 blue:0.748 alpha:1.0];
314 [colors2 addObject:(id)[color CGColor]];
315 self.highlightGradientColors = colors2;
316 self.highlightGradientLocations = [NSMutableArray arrayWithObjects:
317 [NSNumber numberWithFloat:0.0f],
318 [NSNumber numberWithFloat:1.0f],
319 [NSNumber numberWithFloat:0.957f],
320 [NSNumber numberWithFloat:0.574f],
321 [NSNumber numberWithFloat:0.541],
322 [NSNumber numberWithFloat:0.185],
323 [NSNumber numberWithFloat:0.812f],
324 nil];
325 [self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
326 [self setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
327 }
328 - (void)useSimpleOrangeStyle
329 {
330 NSMutableArray *colors = [NSMutableArray arrayWithCapacity:2];
331 UIColor *color = [UIColor colorWithRed:0.935 green:0.403 blue:0.02 alpha:1.0];
332 [colors addObject:(id)[color CGColor]];
333 color = [UIColor colorWithRed:0.97 green:0.582 blue:0.0 alpha:1.0];
334 [colors addObject:(id)[color CGColor]];
335 self.normalGradientColors = colors;
336 self.normalGradientLocations = [NSMutableArray arrayWithObjects:
337 [NSNumber numberWithFloat:0.0f],
338 [NSNumber numberWithFloat:1.0f],
339 nil];
340
341 NSMutableArray *colors2 = [NSMutableArray arrayWithCapacity:3];
342 color = [UIColor colorWithRed:0.914 green:0.309 blue:0.0 alpha:1.0];
343 [colors2 addObject:(id)[color CGColor]];
344 color = [UIColor colorWithRed:0.935 green:0.4 blue:0.0 alpha:1.0];
345 [colors2 addObject:(id)[color CGColor]];
346 color = [UIColor colorWithRed:0.946 green:0.441 blue:0.01 alpha:1.0];
347 [colors2 addObject:(id)[color CGColor]];
348 self.highlightGradientColors = colors2;
349 self.highlightGradientLocations = [NSMutableArray arrayWithObjects:
350 [NSNumber numberWithFloat:0.0f],
351 [NSNumber numberWithFloat:1.0f],
352 [NSNumber numberWithFloat:0.498f],
353 nil];
354
355 self.cornerRadius = 9.f;
356 [self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
357 }
358 - (void)useGreenConfirmStyle
359 {
360 NSMutableArray *colors = [NSMutableArray arrayWithCapacity:5];
361 UIColor *color = [UIColor colorWithRed:0.15 green:0.667 blue:0.152 alpha:1.0];
362 [colors addObject:(id)[color CGColor]];
363 color = [UIColor colorWithRed:0.566 green:0.841 blue:0.566 alpha:1.0];
364 [colors addObject:(id)[color CGColor]];
365 color = [UIColor colorWithRed:0.341 green:0.75 blue:0.345 alpha:1.0];
366 [colors addObject:(id)[color CGColor]];
367 color = [UIColor colorWithRed:0.0 green:0.592 blue:0.0 alpha:1.0];
368 [colors addObject:(id)[color CGColor]];
369 color = [UIColor colorWithRed:0.0 green:0.592 blue:0.0 alpha:1.0];
370 [colors addObject:(id)[color CGColor]];
371 self.normalGradientColors = colors;
372 self.normalGradientLocations = [NSMutableArray arrayWithObjects:
373 [NSNumber numberWithFloat:0.0f],
374 [NSNumber numberWithFloat:1.0f],
375 [NSNumber numberWithFloat:0.582f],
376 [NSNumber numberWithFloat:0.418f],
377 [NSNumber numberWithFloat:0.346],
378 nil];
379
380 NSMutableArray *colors2 = [NSMutableArray arrayWithCapacity:5];
381 color = [UIColor colorWithRed:0.009 green:0.467 blue:0.005 alpha:1.0];
382 [colors2 addObject:(id)[color CGColor]];
383 color = [UIColor colorWithRed:0.562 green:0.754 blue:0.562 alpha:1.0];
384 [colors2 addObject:(id)[color CGColor]];
385 color = [UIColor colorWithRed:0.212 green:0.543 blue:0.212 alpha:1.0];
386 [colors2 addObject:(id)[color CGColor]];
387 color = [UIColor colorWithRed:0.153 green:0.5 blue:0.152 alpha:1.0];
388 [colors2 addObject:(id)[color CGColor]];
389 color = [UIColor colorWithRed:0.004 green:0.388 blue:0.0 alpha:1.0];
390 [colors addObject:(id)[color CGColor]];
391
392 self.highlightGradientColors = colors;
393 self.highlightGradientLocations = [NSMutableArray arrayWithObjects:
394 [NSNumber numberWithFloat:0.0f],
395 [NSNumber numberWithFloat:1.0f],
396 [NSNumber numberWithFloat:0.715f],
397 [NSNumber numberWithFloat:0.513f],
398 [NSNumber numberWithFloat:0.445f],
399 nil];
400 self.cornerRadius = 9.f;
401 [self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
402
403 }
404 #pragma mark -
405 - (void)drawRect:(CGRect)rect
406 {
407 self.backgroundColor = [UIColor clearColor];
408 CGRect imageBounds = CGRectMake(0.0, 0.0, self.bounds.size.width - 0.5, self.bounds.size.height);
409
410
411 CGGradientRef gradient;
412 CGContextRef context = UIGraphicsGetCurrentContext();
413 CGPoint point2;
414
415 CGFloat resolution = 0.5 * (self.bounds.size.width / imageBounds.size.width + self.bounds.size.height / imageBounds.size.height);
416
417 CGFloat stroke = strokeWeight * resolution;
418 if (stroke < 1.0)
419 stroke = ceil(stroke);
420 else
421 stroke = round(stroke);
422 stroke /= resolution;
423 CGFloat alignStroke = fmod(0.5 * stroke * resolution, 1.0);
424 CGMutablePathRef path = CGPathCreateMutable();
425 CGPoint point = CGPointMake((self.bounds.size.width - [self cornerRadius]), self.bounds.size.height - 0.5f);
426 point.x = (round(resolution * point.x + alignStroke) - alignStroke) / resolution;
427 point.y = (round(resolution * point.y + alignStroke) - alignStroke) / resolution;
428 CGPathMoveToPoint(path, NULL, point.x, point.y);
429 point = CGPointMake(self.bounds.size.width - 0.5f, (self.bounds.size.height - [self cornerRadius]));
430 point.x = (round(resolution * point.x + alignStroke) - alignStroke) / resolution;
431 point.y = (round(resolution * point.y + alignStroke) - alignStroke) / resolution;
432 CGPoint controlPoint1 = CGPointMake((self.bounds.size.width - ([self cornerRadius] / 2.f)), self.bounds.size.height - 0.5f);
433 controlPoint1.x = (round(resolution * controlPoint1.x + alignStroke) - alignStroke) / resolution;
434 controlPoint1.y = (round(resolution * controlPoint1.y + alignStroke) - alignStroke) / resolution;
435 CGPoint controlPoint2 = CGPointMake(self.bounds.size.width - 0.5f, (self.bounds.size.height - ([self cornerRadius] / 2.f)));
436 controlPoint2.x = (round(resolution * controlPoint2.x + alignStroke) - alignStroke) / resolution;
437 controlPoint2.y = (round(resolution * controlPoint2.y + alignStroke) - alignStroke) / resolution;
438 CGPathAddCurveToPoint(path, NULL, controlPoint1.x, controlPoint1.y, controlPoint2.x, controlPoint2.y, point.x, point.y);
439 point = CGPointMake(self.bounds.size.width - 0.5f, [self cornerRadius]);
440 point.x = (round(resolution * point.x + alignStroke) - alignStroke) / resolution;
441 point.y = (round(resolution * point.y + alignStroke) - alignStroke) / resolution;
442 CGPathAddLineToPoint(path, NULL, point.x, point.y);
443 point = CGPointMake((self.bounds.size.width - [self cornerRadius]), 0.0);
444 point.x = (round(resolution * point.x + alignStroke) - alignStroke) / resolution;
445 point.y = (round(resolution * point.y + alignStroke) - alignStroke) / resolution;
446 controlPoint1 = CGPointMake(self.bounds.size.width - 0.5f, ([self cornerRadius] / 2.f));
447 controlPoint1.x = (round(resolution * controlPoint1.x + alignStroke) - alignStroke) / resolution;
448 controlPoint1.y = (round(resolution * controlPoint1.y + alignStroke) - alignStroke) / resolution;
449 controlPoint2 = CGPointMake((self.bounds.size.width - ([self cornerRadius] / 2.f)), 0.0);
450 controlPoint2.x = (round(resolution * controlPoint2.x + alignStroke) - alignStroke) / resolution;
451 controlPoint2.y = (round(resolution * controlPoint2.y + alignStroke) - alignStroke) / resolution;
452 CGPathAddCurveToPoint(path, NULL, controlPoint1.x, controlPoint1.y, controlPoint2.x, controlPoint2.y, point.x, point.y);
453 point = CGPointMake([self cornerRadius], 0.0);
454 point.x = (round(resolution * point.x + alignStroke) - alignStroke) / resolution;
455 point.y = (round(resolution * point.y + alignStroke) - alignStroke) / resolution;
456 CGPathAddLineToPoint(path, NULL, point.x, point.y);
457 point = CGPointMake(0.0, [self cornerRadius]);
458 point.x = (round(resolution * point.x + alignStroke) - alignStroke) / resolution;
459 point.y = (round(resolution * point.y + alignStroke) - alignStroke) / resolution;
460 controlPoint1 = CGPointMake(([self cornerRadius] / 2.f), 0.0);
461 controlPoint1.x = (round(resolution * controlPoint1.x + alignStroke) - alignStroke) / resolution;
462 controlPoint1.y = (round(resolution * controlPoint1.y + alignStroke) - alignStroke) / resolution;
463 controlPoint2 = CGPointMake(0.0, ([self cornerRadius] / 2.f));
464 controlPoint2.x = (round(resolution * controlPoint2.x + alignStroke) - alignStroke) / resolution;
465 controlPoint2.y = (round(resolution * controlPoint2.y + alignStroke) - alignStroke) / resolution;
466 CGPathAddCurveToPoint(path, NULL, controlPoint1.x, controlPoint1.y, controlPoint2.x, controlPoint2.y, point.x, point.y);
467 point = CGPointMake(0.0, (self.bounds.size.height - [self cornerRadius]));
468 point.x = (round(resolution * point.x + alignStroke) - alignStroke) / resolution;
469 point.y = (round(resolution * point.y + alignStroke) - alignStroke) / resolution;
470 CGPathAddLineToPoint(path, NULL, point.x, point.y);
471 point = CGPointMake([self cornerRadius], self.bounds.size.height - 0.5f);
472 point.x = (round(resolution * point.x + alignStroke) - alignStroke) / resolution;
473 point.y = (round(resolution * point.y + alignStroke) - alignStroke) / resolution;
474 controlPoint1 = CGPointMake(0.0, (self.bounds.size.height - ([self cornerRadius] / 2.f)));
475 controlPoint1.x = (round(resolution * controlPoint1.x + alignStroke) - alignStroke) / resolution;
476 controlPoint1.y = (round(resolution * controlPoint1.y + alignStroke) - alignStroke) / resolution;
477 controlPoint2 = CGPointMake(([self cornerRadius] / 2.f), self.bounds.size.height - 0.5f);
478 controlPoint2.x = (round(resolution * controlPoint2.x + alignStroke) - alignStroke) / resolution;
479 controlPoint2.y = (round(resolution * controlPoint2.y + alignStroke) - alignStroke) / resolution;
480 CGPathAddCurveToPoint(path, NULL, controlPoint1.x, controlPoint1.y, controlPoint2.x, controlPoint2.y, point.x, point.y);
481 point = CGPointMake((self.bounds.size.width - [self cornerRadius]), self.bounds.size.height - 0.5f);
482 point.x = (round(resolution * point.x + alignStroke) - alignStroke) / resolution;
483 point.y = (round(resolution * point.y + alignStroke) - alignStroke) / resolution;
484 CGPathAddLineToPoint(path, NULL, point.x, point.y);
485 CGPathCloseSubpath(path);
486 if (self.state == UIControlStateHighlighted)
487 gradient = self.highlightGradient;
488 else
489 gradient = self.normalGradient;
490
491 CGContextAddPath(context, path);
492 CGContextSaveGState(context);
493 CGContextEOClip(context);
494 point = CGPointMake((self.bounds.size.width / 2.0), self.bounds.size.height - 0.5f);
495 point2 = CGPointMake((self.bounds.size.width / 2.0), 0.0);
496 CGContextDrawLinearGradient(context, gradient, point, point2, (kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation));
497 CGContextRestoreGState(context);
498 [strokeColor setStroke];
499 CGContextSetLineWidth(context, stroke);
500 CGContextSetLineCap(context, kCGLineCapSquare);
501 CGContextAddPath(context, path);
502 CGContextStrokePath(context);
503 CGPathRelease(path);
504
505 }
506 #pragma mark -
507 #pragma mark Touch Handling
508 - (void)hesitateUpdate
509 {
510 [self setNeedsDisplay];
511 }
512 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
513 {
514 [super touchesBegan:touches withEvent:event];
515 [self setNeedsDisplay];
516 }
517 - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
518 {
519 [super touchesCancelled:touches withEvent:event];
520 [self setNeedsDisplay];
521 [self performSelector:@selector(hesitateUpdate) withObject:nil afterDelay:0.1];
522 }
523 - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
524 {
525 [super touchesMoved:touches withEvent:event];
526 [self setNeedsDisplay];
527
528 }
529 - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
530 {
531 [super touchesEnded:touches withEvent:event];
532 [self setNeedsDisplay];
533 [self performSelector:@selector(hesitateUpdate) withObject:nil afterDelay:0.1];
534 }
535 #pragma mark -
536 #pragma mark NSCoding
537 - (void)encodeWithCoder:(NSCoder *)encoder
538 {
539 [super encodeWithCoder:encoder];
540 [encoder encodeObject:[self normalGradientColors] forKey:@"normalGradientColors"];
541 [encoder encodeObject:[self normalGradientLocations] forKey:@"normalGradientLocations"];
542 [encoder encodeObject:[self highlightGradientColors] forKey:@"highlightGradientColors"];
543 [encoder encodeObject:[self highlightGradientLocations] forKey:@"highlightGradientLocations"];
544 }
545
546 - (id)initWithCoder:(NSCoder *)decoder
547 {
548 if ((self = [super initWithCoder:decoder]))
549 {
550 [self setNormalGradientColors:[decoder decodeObjectForKey:@"normalGradientColors"]];
551 [self setNormalGradientLocations:[decoder decodeObjectForKey:@"normalGradientLocations"]];
552 [self setHighlightGradientColors:[decoder decodeObjectForKey:@"highlightGradientColors"]];
553 [self setHighlightGradientLocations:[decoder decodeObjectForKey:@"highlightGradientLocations"]];
554 self.strokeColor = [UIColor colorWithRed:0.076 green:0.103 blue:0.195 alpha:1.0];
555 self.strokeWeight = 1.0;
556
557 if (self.normalGradientColors == nil)
558 [self useWhiteStyle];
559
560 [self setOpaque:NO];
561 self.backgroundColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.0];
562 }
563 return self;
564 }
565 #pragma mark -
566 - (void)dealloc
567 {
568 [normalGradientColors release];
569 [normalGradientLocations release];
570 [highlightGradientColors release];
571 [highlightGradientLocations release];
572 [strokeColor release];
573
574 if (normalGradient != NULL)
575 CGGradientRelease(normalGradient);
576 if (highlightGradient != NULL)
577 CGGradientRelease(highlightGradient);
578
579
580 [super dealloc];
581 }
582
583 @end
Something went wrong with that request. Please try again.