forked from soffes/sstoolkit
-
Notifications
You must be signed in to change notification settings - Fork 100
/
TWGradientView.m
216 lines (169 loc) · 6.56 KB
/
TWGradientView.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
//
// TWGradientView.m
// TWToolkit
//
// Created by Sam Soffes on 10/27/09.
// Copyright 2009 Tasteful Works, Inc. All rights reserved.
//
#import "TWGradientView.h"
@interface TWGradientView (PrivateMethods)
- (void)_refreshGradient;
@end
@implementation TWGradientView
@synthesize topColor = _topColor;
@synthesize bottomColor = _bottomColor;
@synthesize topBorderColor = _topBorderColor;
@synthesize bottomBorderColor = _bottomBorderColor;
@synthesize topInsetAlpha = _topInsetAlpha;
@synthesize bottomInsetAlpha = _bottomInsetAlpha;
@synthesize hasTopBorder = _hasTopBorder;
@synthesize hasBottomBorder = _hasBottomBorder;
@synthesize showsInsets = _showsInsets;
#pragma mark -
#pragma mark Class Methods
#pragma mark -
+ (UIColor *)defaultTopColor {
return [UIColor colorWithRed:0.676 green:0.722 blue:0.765 alpha:1.0];
}
+ (UIColor *)defaultBottomColor {
return [UIColor colorWithRed:0.514 green:0.568 blue:0.617 alpha:1.0];
}
+ (UIColor *)defaultTopBorderColor {
return [UIColor colorWithRed:0.558 green:0.599 blue:0.643 alpha:1.0];
}
+ (UIColor *)defaultBottomBorderColor {
return [UIColor colorWithRed:0.428 green:0.479 blue:0.520 alpha:1.0];
}
+ (CGFloat)defaultTopInsetAlpha {
return 0.3;
}
+ (CGFloat)defaultBottomInsetAlpha {
return 0.0;
}
#pragma mark -
#pragma mark NSObject
#pragma mark -
- (void)dealloc {
if (_hasDrawn) {
[self removeObserver:self forKeyPath:@"topColor"];
[self removeObserver:self forKeyPath:@"bottomColor"];
[self removeObserver:self forKeyPath:@"topBorderColor"];
[self removeObserver:self forKeyPath:@"bottomBorderColor"];
[self removeObserver:self forKeyPath:@"topInsetAlpha"];
[self removeObserver:self forKeyPath:@"bottomInsetAlpha"];
[self removeObserver:self forKeyPath:@"hasTopBorder"];
[self removeObserver:self forKeyPath:@"hasBottomBorder"];
[self removeObserver:self forKeyPath:@"showsInsets"];
CGGradientRelease(_gradient);
}
self.topColor = nil;
self.bottomColor = nil;
self.topBorderColor = nil;
self.bottomBorderColor = nil;
[super dealloc];
}
#pragma mark -
#pragma mark UIView
#pragma mark -
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
self.opaque = YES;
// Defaults
self.topColor = [TWGradientView defaultTopColor];
self.bottomColor = [TWGradientView defaultBottomColor];
self.topBorderColor = [TWGradientView defaultTopBorderColor];
self.bottomBorderColor = [TWGradientView defaultBottomBorderColor];
self.topInsetAlpha = [TWGradientView defaultTopInsetAlpha];
self.bottomInsetAlpha = [TWGradientView defaultBottomInsetAlpha];
self.hasTopBorder = YES;
self.hasBottomBorder = YES;
self.showsInsets = YES;
_hasDrawn = NO;
_gradient = nil;
}
return self;
}
- (void)drawRect:(CGRect)rect {
if (!_hasDrawn) {
// Add observers
[self addObserver:self forKeyPath:@"topColor" options:NSKeyValueObservingOptionNew context:nil];
[self addObserver:self forKeyPath:@"bottomColor" options:NSKeyValueObservingOptionNew context:nil];
[self addObserver:self forKeyPath:@"topBorderColor" options:NSKeyValueObservingOptionNew context:nil];
[self addObserver:self forKeyPath:@"bottomBorderColor" options:NSKeyValueObservingOptionNew context:nil];
[self addObserver:self forKeyPath:@"topInsetAlpha" options:NSKeyValueObservingOptionNew context:nil];
[self addObserver:self forKeyPath:@"bottomInsetAlpha" options:NSKeyValueObservingOptionNew context:nil];
[self addObserver:self forKeyPath:@"hasTopBorder" options:NSKeyValueObservingOptionNew context:nil];
[self addObserver:self forKeyPath:@"hasBottomBorder" options:NSKeyValueObservingOptionNew context:nil];
[self addObserver:self forKeyPath:@"showsInsets" options:NSKeyValueObservingOptionNew context:nil];
// Draw gradient
[self _refreshGradient];
_hasDrawn = YES;
}
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
CGContextClipToRect(context, rect);
// Gradient
CGPoint start = CGPointMake(0.0, 0.0);
CGPoint end = CGPointMake(0.0, rect.size.height);
CGContextDrawLinearGradient(context, _gradient, start, end, 0);
CGContextSetLineWidth(context, 2.0);
if (_hasTopBorder) {
// Top inset
if (_showsInsets && _topInsetAlpha > 0.0) {
CGContextSetStrokeColorWithColor(context, [UIColor colorWithWhite:1.0 alpha:_topInsetAlpha].CGColor);
CGContextMoveToPoint(context, 0.0, 1.0);
CGContextAddLineToPoint(context, rect.size.width, 1.0);
CGContextStrokePath(context);
}
// Top border
CGContextSetStrokeColorWithColor(context, _topBorderColor.CGColor);
CGContextMoveToPoint(context, 0.0, 0.0);
CGContextAddLineToPoint(context, rect.size.width, 0.0);
CGContextStrokePath(context);
}
if (_hasBottomBorder) {
// Bottom inset
if (_showsInsets && _bottomInsetAlpha > 0.0) {
CGContextSetStrokeColorWithColor(context, [UIColor colorWithWhite:1.0 alpha:_bottomInsetAlpha].CGColor);
CGContextMoveToPoint(context, 0.0, rect.size.height - 1.0);
CGContextAddLineToPoint(context, rect.size.width, rect.size.height - 1.0);
CGContextStrokePath(context);
}
// Bottom border
CGContextSetStrokeColorWithColor(context, _bottomBorderColor.CGColor);
CGContextMoveToPoint(context, 0.0, rect.size.height);
CGContextAddLineToPoint(context, rect.size.width, rect.size.height);
CGContextStrokePath(context);
}
}
#pragma mark -
#pragma mark Gradient Methods
#pragma mark -
- (void)_refreshGradient {
// TODO: Automatically convert colors into the same colorspace
CGColorSpaceRef colorSpace = CGColorGetColorSpace(_topColor.CGColor);
NSArray *colors = [NSArray arrayWithObjects:(id)_topColor.CGColor, (id)_bottomColor.CGColor, nil];
CGGradientRelease(_gradient);
_gradient = CGGradientCreateWithColors(colorSpace, (CFArrayRef)colors, NULL);
[self setNeedsDisplay];
}
#pragma mark -
#pragma mark Observer
#pragma mark -
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
// Update the gradient and redraw if gradient colors changed
if ([keyPath isEqualToString:@"topColor"] || [keyPath isEqualToString:@"bottomColor"]) {
[self _refreshGradient];
return;
}
// Redraw if colors or borders changed
if ([keyPath isEqualToString:@"topBorderColor"] || [keyPath isEqualToString:@"bottomBorderColor"] ||
[keyPath isEqualToString:@"topInsetAlpha"] || [keyPath isEqualToString:@"bottomInsetAlpha"] ||
[keyPath isEqualToString:@"hasTopBorder"] || [keyPath isEqualToString:@"hasBottomBorder"] ||
[keyPath isEqualToString:@"showsInsets"]) {
[self setNeedsDisplay];
return;
}
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
@end