-
Notifications
You must be signed in to change notification settings - Fork 162
/
DCRoundSwitchKnobLayer.m
70 lines (59 loc) · 2.64 KB
/
DCRoundSwitchKnobLayer.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
//
// DCRoundSwitchKnobLayer.m
//
// Created by Patrick Richards on 29/06/11.
// MIT License.
//
// http://twitter.com/patr
// http://domesticcat.com.au/projects
// http://github.com/domesticcatsoftware/DCRoundSwitch
//
#import "DCRoundSwitchKnobLayer.h"
CGGradientRef CreateGradientRefWithColors(CGColorSpaceRef colorSpace, CGColorRef startColor, CGColorRef endColor);
@implementation DCRoundSwitchKnobLayer
@synthesize gripped;
- (void)drawInContext:(CGContextRef)context
{
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
CGRect knobRect = CGRectInset(self.bounds, 2, 2);
CGFloat knobRadius = self.bounds.size.height - 2;
// knob outline (shadow is drawn in the toggle layer)
CGContextSetStrokeColorWithColor(context, [UIColor colorWithWhite:0.62 alpha:1.0].CGColor);
CGContextSetLineWidth(context, 1.5);
CGContextStrokeEllipseInRect(context, knobRect);
CGContextSetShadowWithColor(context, CGSizeMake(0, 0), 0, NULL);
// knob inner gradient
CGContextAddEllipseInRect(context, knobRect);
CGContextClip(context);
CGColorRef knobStartColor = [UIColor colorWithWhite:0.82 alpha:1.0].CGColor;
CGColorRef knobEndColor = (self.gripped) ? [UIColor colorWithWhite:0.894 alpha:1.0].CGColor : [UIColor colorWithWhite:0.996 alpha:1.0].CGColor;
CGPoint topPoint = CGPointMake(0, 0);
CGPoint bottomPoint = CGPointMake(0, knobRadius + 2);
CGGradientRef knobGradient = CreateGradientRefWithColors(colorSpace, knobStartColor, knobEndColor);
CGContextDrawLinearGradient(context, knobGradient, topPoint, bottomPoint, 0);
CGGradientRelease(knobGradient);
// knob inner highlight
CGContextAddEllipseInRect(context, CGRectInset(knobRect, 0.5, 0.5));
CGContextAddEllipseInRect(context, CGRectInset(knobRect, 1.5, 1.5));
CGContextEOClip(context);
CGGradientRef knobHighlightGradient = CreateGradientRefWithColors(colorSpace, [UIColor whiteColor].CGColor, [UIColor colorWithWhite:1.0 alpha:0.5].CGColor);
CGContextDrawLinearGradient(context, knobHighlightGradient, topPoint, bottomPoint, 0);
CGGradientRelease(knobHighlightGradient);
CGColorSpaceRelease(colorSpace);
}
CGGradientRef CreateGradientRefWithColors(CGColorSpaceRef colorSpace, CGColorRef startColor, CGColorRef endColor)
{
CGFloat colorStops[2] = {0.0, 1.0};
CGColorRef colors[] = {startColor, endColor};
CFArrayRef colorsArray = CFArrayCreate(NULL, (const void**)colors, sizeof(colors) / sizeof(CGColorRef), &kCFTypeArrayCallBacks);
CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, colorsArray, colorStops);
[NSMakeCollectable(gradient) autorelease];
CFRelease(colorsArray);
return gradient;
}
- (void)setGripped:(BOOL)newGripped
{
gripped = newGripped;
[self setNeedsDisplay];
}
@end