-
Notifications
You must be signed in to change notification settings - Fork 96
Expand file tree
/
Copy pathSAMGradientView.m
More file actions
334 lines (253 loc) · 8.82 KB
/
SAMGradientView.m
File metadata and controls
334 lines (253 loc) · 8.82 KB
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
//
// SAMGradientView.m
// SAMGradientView
//
// Created by Sam Soffes on 10/27/09.
// Copyright (c) 2009-2013 Sam Soffes. All rights reserved.
//
#import "SAMGradientView.h"
@interface SAMGradientView ()
@property (nonatomic) CGGradientRef gradient;
@end
@implementation SAMGradientView
#pragma mark - Accessors
@synthesize gradient = _gradient;
@synthesize gradientColors = _gradientColors;
@synthesize gradientLocations = _gradientLocations;
@synthesize gradientDirection = _gradientDirection;
@synthesize useThinBorders = _useThinBorders;
@synthesize topBorderColor = _topBorderColor;
@synthesize topInsetColor = _topInsetColor;
@synthesize rightBorderColor = _rightBorderColor;
@synthesize rightInsetColor = _rightInsetColor;
@synthesize bottomBorderColor = _bottomBorderColor;
@synthesize bottomInsetColor = _bottomInsetColor;
@synthesize leftBorderColor = _leftBorderColor;
@synthesize leftInsetColor = _leftInsetColor;
@synthesize gradientStartPoint = _gradientStartPoint;
@synthesize gradientEndPoint = _gradientEndPoint;
#ifdef __IPHONE_7_0
@synthesize dimmedGradientColors = _dimmedGradientColors;
#endif
- (void)setGradient:(CGGradientRef)gradient {
if (_gradient) {
CGGradientRelease(_gradient);
}
_gradient = gradient;
[self setNeedsDisplay];
}
- (void)setGradientColors:(NSArray *)colors {
_gradientColors = colors;
[self refreshGradient];
}
- (void)setGradientLocations:(NSArray *)locations {
_gradientLocations = locations;
[self refreshGradient];
}
- (void)setGradientDirection:(SAMGradientViewDirection)direction {
_gradientDirection = direction;
[self setNeedsDisplay];
}
#ifdef __IPHONE_7_0
- (NSArray *)dimmedGradientColors {
if (!_dimmedGradientColors) {
NSMutableArray *dimmed = [self.gradientColors mutableCopy];
[dimmed enumerateObjectsUsingBlock:^(UIColor *color, NSUInteger index, BOOL *stop) {
CGFloat hue, saturation, brightness, alpha;
[color getHue:&hue saturation:&saturation brightness:&brightness alpha:&alpha];
[dimmed replaceObjectAtIndex:index withObject:[UIColor colorWithHue:hue saturation:0.0f brightness:brightness alpha:alpha]];
}];
return dimmed;
}
return _dimmedGradientColors;
}
- (void)setDimmedGradientColors:(NSArray *)colors {
_dimmedGradientColors = colors;
[self refreshGradient];
}
#endif
- (void)setUseThinBorders:(BOOL)useThinBorders {
_useThinBorders = useThinBorders;
[self setNeedsDisplay];
}
- (void)setTopBorderColor:(UIColor *)topBorderColor {
_topBorderColor = topBorderColor;
[self setNeedsDisplay];
}
- (void)setTopInsetColor:(UIColor *)topInsetColor {
_topInsetColor = topInsetColor;
[self setNeedsDisplay];
}
- (void)setRightBorderColor:(UIColor *)rightBorderColor {
_rightBorderColor = rightBorderColor;
[self setNeedsDisplay];
}
- (void)setRightInsetColor:(UIColor *)rightInsetColor {
_rightInsetColor = rightInsetColor;
[self setNeedsDisplay];
}
- (void)setBottomBorderColor:(UIColor *)bottomBorderColor {
_bottomBorderColor = bottomBorderColor;
[self setNeedsDisplay];
}
- (void)setBottomInsetColor:(UIColor *)bottomInsetColor {
_bottomInsetColor = bottomInsetColor;
[self setNeedsDisplay];
}
- (void)setLeftBorderColor:(UIColor *)leftBorderColor {
_leftBorderColor = leftBorderColor;
[self setNeedsDisplay];
}
- (void)setLeftInsetColor:(UIColor *)leftInsetColor {
_leftInsetColor = leftInsetColor;
[self setNeedsDisplay];
}
#pragma mark - NSObject
- (void)dealloc {
self.gradient = nil;
}
#pragma mark - UIView
- (id)initWithCoder:(NSCoder *)aDecoder {
if ((self = [super initWithCoder:aDecoder])) {
[self initialize];
}
return self;
}
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
[self initialize];
}
return self;
}
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGSize size = self.bounds.size;
CGFloat borderWidth = self.useThinBorders ? 1.0f / [[UIScreen mainScreen] scale] : 1.0f;
// Gradient
if (self.gradient) {
CGPoint start = CGPointMake(0.0f, 0.0f);
CGPoint end;
switch (self.gradientDirection) {
case SAMGradientViewDirectionVertical:
end = CGPointMake(0.0f, size.height);
CGContextDrawLinearGradient(context, self.gradient, start, end, kNilOptions);
break;
case SAMGradientViewDirectionHorizontal:
end = CGPointMake(size.width, 0.0f);
CGContextDrawLinearGradient(context, self.gradient, start, end, kNilOptions);
break;
case SAMGradientViewDirectionNone:
CGContextDrawLinearGradient(context, self.gradient, self.gradientStartPoint, self.gradientEndPoint, kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation);
break;
}
}
// Top
if (self.topBorderColor) {
// Top border
CGContextSetFillColorWithColor(context, self.topBorderColor.CGColor);
CGContextFillRect(context, CGRectMake(0.0f, 0.0f, size.width, borderWidth));
// Top inset
if (self.topInsetColor) {
CGContextSetFillColorWithColor(context, self.topInsetColor.CGColor);
CGContextFillRect(context, CGRectMake(0.0f, borderWidth, size.width, borderWidth));
}
}
CGFloat sideY = self.topBorderColor ? borderWidth : 0.0f;
CGFloat sideHeight = size.height;
if (self.topBorderColor) {
sideHeight -= borderWidth;
}
if (self.bottomBorderColor) {
sideHeight -= borderWidth;
}
// Right
if (self.rightBorderColor) {
// Right inset
if (self.rightInsetColor) {
CGContextSetFillColorWithColor(context, self.rightInsetColor.CGColor);
CGContextFillRect(context, CGRectMake(size.width - borderWidth - borderWidth, sideY, borderWidth, sideHeight));
}
// Right border
CGContextSetFillColorWithColor(context, self.rightBorderColor.CGColor);
CGContextFillRect(context, CGRectMake(size.width - borderWidth, sideY, borderWidth, sideHeight));
}
// Bottom
if (self.bottomBorderColor) {
// Bottom inset
if (self.bottomInsetColor) {
CGContextSetFillColorWithColor(context, self.bottomInsetColor.CGColor);
CGContextFillRect(context, CGRectMake(0.0f, rect.size.height - borderWidth - borderWidth, size.width, borderWidth));
}
// Bottom border
CGContextSetFillColorWithColor(context, self.bottomBorderColor.CGColor);
CGContextFillRect(context, CGRectMake(0.0f, rect.size.height - borderWidth, size.width, borderWidth));
}
// Left
if (self.leftBorderColor) {
// Left inset
if (self.leftInsetColor) {
CGContextSetFillColorWithColor(context, self.leftInsetColor.CGColor);
CGContextFillRect(context, CGRectMake(borderWidth, sideY, borderWidth, sideHeight));
}
// Left border
CGContextSetFillColorWithColor(context, self.leftBorderColor.CGColor);
CGContextFillRect(context, CGRectMake(0.0f, sideY, borderWidth, sideHeight));
}
}
#ifdef __IPHONE_7_0
- (void)tintColorDidChange {
[super tintColorDidChange];
[self refreshGradient];
}
#endif
#pragma mark - Private
- (void)initialize {
self.contentMode = UIViewContentModeRedraw;
}
- (void)refreshGradient {
#ifdef __IPHONE_7_0
if ([self respondsToSelector:@selector(tintAdjustmentMode)] && self.tintAdjustmentMode == UIViewTintAdjustmentModeDimmed) {
NSArray *locations = self.dimmedGradientColors.count == self.gradientLocations.count ? self.gradientLocations : nil;
self.gradient = SAMGradientCreateWithColorsAndLocations(self.dimmedGradientColors, locations);
return;
}
#endif
self.gradient = SAMGradientCreateWithColorsAndLocations(self.gradientColors, self.gradientLocations);
}
@end
#pragma mark - Drawing Functions
CGGradientRef SAMGradientCreateWithColors(NSArray *colors) {
return SAMGradientCreateWithColorsAndLocations(colors, nil);
}
CGGradientRef SAMGradientCreateWithColorsAndLocations(NSArray *colors, NSArray *locations) {
NSUInteger colorsCount = [colors count];
if (colorsCount < 2) {
return nil;
}
CGColorSpaceRef colorSpace = CGColorGetColorSpace([[colors objectAtIndex:0] CGColor]);
NSMutableArray *gradientColors = [[NSMutableArray alloc] initWithCapacity:colorsCount];
[colors enumerateObjectsUsingBlock:^(id object, NSUInteger index, BOOL *stop) {
[gradientColors addObject:(id)[(UIColor *)object CGColor]];
}];
CGFloat *gradientLocations = NULL;
NSUInteger locationsCount = [locations count];
if (locationsCount == colorsCount) {
gradientLocations = (CGFloat *)malloc(sizeof(CGFloat) * locationsCount);
[locations enumerateObjectsUsingBlock:^(id object, NSUInteger index, BOOL *stop) {
gradientLocations[index] = [object floatValue];
}];
}
CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef)gradientColors, gradientLocations);
if (gradientLocations) {
free(gradientLocations);
}
return gradient;
}
void SAMDrawGradientInRect(CGContextRef context, CGGradientRef gradient, CGRect rect) {
CGContextSaveGState(context);
CGContextClipToRect(context, rect);
CGPoint start = CGPointMake(rect.origin.x, rect.origin.y);
CGPoint end = CGPointMake(rect.origin.x, rect.origin.y + rect.size.height);
CGContextDrawLinearGradient(context, gradient, start, end, 0);
CGContextRestoreGState(context);
}