-
Notifications
You must be signed in to change notification settings - Fork 3
/
JDMColorControlObject.m
406 lines (254 loc) · 10.8 KB
/
JDMColorControlObject.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
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
//
// JDMColorControlObject.m
// CIFilter
//
// Created by Justin Madewell on 11/22/15.
// Copyright © 2015 Justin Madewell. All rights reserved.
//
#import "JDMColorControlObject.h"
@interface JDMColorControlObject ()
@property CGRect bounds;
@property UIView *superView;
@property UISlider *redSlider;
@property UISlider *greenSlider;
@property UISlider *blueSlider;
@property UISlider *alphaSlider;
@property UILabel *descriptionLabel;
@property UIView *expandedView;
@property UIView *buttonView;
@property NSString *colorTitle;
@property NSString *colorDescription;
@property CIColor *defaultCIColor;
@property CGFloat commonHeight;
@property BOOL isSliding;
@property UILabel *buttonLabel;
@end
@implementation JDMColorControlObject
-(id)initWithDelegate:(id<JDMColorControlObjectDelegate>)delegate inView:(UIView *)view withDefaultRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue andAlpha:(CGFloat)alpha withTitle:(NSString *)title description:(NSString *)description andKeyValue:(NSString *)keyValue
{
self = [super init];
if(self)
{
self.delegate = delegate;
self.keyValue = keyValue;
self.superView = view;
self.colorTitle = title;
self.colorDescription = description;
self.defaultCIColor = [CIColor colorWithRed:red green:green blue:blue alpha:alpha];
self.positionY = 20;
[self setup];
}
return self;
}
-(void)updateButtonPosition:(CGFloat)buttonY
{
[self updateLabelColor];
// update the views colors
[UIView animateWithDuration:0 animations:^{
self.buttonView.center = CGPointMake(self.superView.center.x, buttonY);
}];
}
-(void)setup
{
// Set Defaults
self.bounds = self.superView.bounds;
self.color = [UIColor colorWithCIColor:self.defaultCIColor];
[self makeExpandedView];
self.buttonView = [self makeColorControlButtonwithDisplayName:self.colorTitle];
[self.superView addSubview:self.buttonView];
self.buttonView.center = CGPointMake(self.superView.center.x, self.positionY);
[self updateLabelColor];
self.isSliding = NO;
}
-(void)makeExpandedView
{
CIColor *ci_color = [CIColor colorWithCGColor:self.color.CGColor];
self.expandedView = [self makeColorControlExpandedView];
self.redSlider = [self makeColorSliderWithTitle:@"RED" withDefault:ci_color.red];
self.greenSlider = [self makeColorSliderWithTitle:@"GREEN" withDefault:ci_color.green];
self.blueSlider = [self makeColorSliderWithTitle:@"BLUE" withDefault:ci_color.blue];
self.alphaSlider = [self makeColorSliderWithTitle:@"ALPHA" withDefault:ci_color.alpha];
self.descriptionLabel = [self makeDescriptionLabel];
[self.expandedView addSubview:self.redSlider];
[self.expandedView addSubview:self.greenSlider];
[self.expandedView addSubview:self.blueSlider];
[self.expandedView addSubview:self.alphaSlider];
[self.expandedView addSubview:self.descriptionLabel];
// Layout Calculations
self.commonHeight = 30;
CGFloat multplyerY = self.commonHeight*1.5;
CGFloat centerX = self.expandedView.frame.size.width/2;
CGFloat centerY = multplyerY;
// Set Positions
self.redSlider.center = CGPointMake(centerX, centerY);
self.greenSlider.center = CGPointMake(centerX, multplyerY*2);
self.blueSlider.center = CGPointMake(centerX,(multplyerY*3));
self.alphaSlider.center = CGPointMake(centerX, (multplyerY*4));
self.descriptionLabel.center = CGPointMake(centerX, (multplyerY*5));
}
-(NSArray*)getAllLabels
{
NSMutableArray *alllabels = [[NSMutableArray alloc]init];
NSArray *sliders = @[self.redSlider,self.greenSlider,self.blueSlider,self.alphaSlider];
for (UISlider *slider in sliders) {
for (UILabel *label in slider.subviews) {
if ([label class] == [UILabel class]) {
[alllabels addObject:label];
}
}
}
[alllabels addObject:self.buttonLabel];
[alllabels addObject:self.descriptionLabel];
return alllabels;
}
-(void)updateLabelColor
{
UIColor *newTextColor = textColorForColor(self.color);
[UIView animateWithDuration:0 animations:^{
for (UILabel *label in [self getAllLabels]) {
label.textColor = newTextColor;
self.buttonView.layer.borderColor = newTextColor.CGColor;
self.expandedView.layer.borderColor = newTextColor.CGColor;
}
}];
}
#pragma mark - Make Color Control
-(UIView*)makeColorControlButtonwithDisplayName:(NSString*)colorDisplayName
{
CGFloat widthValue = (self.bounds.size.width/3)*2;
UIColor *color = [UIColor whiteColor];
CGRect colorControlbuttonRect = CGRectMake(0, 0, widthValue, self.commonHeight*1.25);
UIView *colorControlButtonView = [[UIView alloc]initWithFrame:colorControlbuttonRect];
colorControlButtonView.backgroundColor = [UIColor colorWithCIColor:self.defaultCIColor];
colorControlButtonView.layer.cornerRadius = colorControlbuttonRect.size.height/2;
colorControlButtonView.layer.masksToBounds = YES;
colorControlButtonView.layer.borderColor = color.CGColor;
colorControlButtonView.layer.borderWidth = 2.0;
UILabel *colorDisplayNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, widthValue, self.commonHeight)];
colorDisplayNameLabel.textColor = color;
colorDisplayNameLabel.textAlignment = NSTextAlignmentCenter;
colorDisplayNameLabel.numberOfLines=1;
colorDisplayNameLabel.text = colorDisplayName;
colorDisplayNameLabel.center = colorControlButtonView.center;
[colorControlButtonView addSubview:colorDisplayNameLabel];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleColorButtonTap)];
[colorControlButtonView addGestureRecognizer:tap];
self.buttonLabel = colorDisplayNameLabel;
return colorControlButtonView;
}
-(UILabel*)makeDescriptionLabel
{
CGFloat labelSize = self.bounds.size.width*0.80;
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0 , 0 , labelSize, self.commonHeight*2)];
label.textAlignment = NSTextAlignmentCenter;
label.lineBreakMode = NSLineBreakByWordWrapping;
label.numberOfLines=0;
label.text = self.colorDescription;
return label;
}
#pragma mark - Make Color Slider
-(UISlider*)makeColorSliderWithTitle:(NSString*)sliderTitle withDefault:(CGFloat)defaultValue
{
UIColor *trackTintColor = [UIColor blackColor];
if ([sliderTitle isEqualToString:@"RED"]) {
trackTintColor = [UIColor redColor];
}
if ([sliderTitle isEqualToString:@"BLUE"]) {
trackTintColor = [UIColor blueColor];
}
if ([sliderTitle isEqualToString:@"GREEN"]) {
trackTintColor = [UIColor greenColor];
}
if ([sliderTitle isEqualToString:@"ALPHA"]) {
trackTintColor = [UIColor grayColor];
}
CGFloat sliderSize = (self.bounds.size.width/3)*2;
UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(0 , 0 , sliderSize, self.commonHeight)];
[slider addTarget:self action:@selector(handleColorSlider:) forControlEvents:UIControlEventAllTouchEvents];
slider.minimumTrackTintColor = trackTintColor;
slider.minimumValue = 0;
slider.maximumValue = 1;
slider.value = defaultValue;
CGRect labelRect = CGRectMake(0, -9, sliderSize, 13);
UILabel *title = [[UILabel alloc]initWithFrame:labelRect];
title.textAlignment = NSTextAlignmentCenter;
title.font = [UIFont fontWithName:@"Helvetica" size:12];
title.text = sliderTitle;
[slider addSubview:title];
UILabel *valueLabel = [[UILabel alloc]initWithFrame:labelRect];
valueLabel.textAlignment = NSTextAlignmentRight;
valueLabel.font = [UIFont fontWithName:@"Helvetica" size:12];
valueLabel.text = [@"" stringByAppendingFormat:@"%.02f",slider.value];
[slider addSubview:valueLabel];
return slider;
}
#pragma mark - Color Slider Changed
-(void)handleColorSlider:(UISlider*)slider
{
UILabel *label = (UILabel*)[slider.subviews objectAtIndex:1];
label.text = [@"" stringByAppendingFormat:@"%.02f",slider.value];
CIColor *currentColor = [CIColor colorWithRed:self.redSlider.value green:self.greenSlider.value blue:self.blueSlider.value alpha:self.alphaSlider.value];
[self.delegate didChangeColor:currentColor forKeyValue:self.keyValue];
self.color = [UIColor colorWithCIColor:currentColor];
[self updateLabelColor];
// update the views colors
[UIView animateWithDuration:0 animations:^{
self.expandedView.backgroundColor = self.color;
self.buttonView.backgroundColor = self.color;
}];
}
-(void)handleColorButtonTap
{
[self expandColorControl];
}
#pragma mark - Make Color Control
-(UIView*)makeColorControlExpandedView
{
CGRect colorControlRect = CGRectInset(self.superView.frame, 20, 20);
UIView *colorController = [[UIView alloc]initWithFrame:colorControlRect];
colorController.backgroundColor = self.color;
colorController.layer.cornerRadius = 10;
colorController.layer.masksToBounds = YES;
colorController.layer.borderColor = [UIColor blackColor].CGColor;
colorController.layer.borderWidth = 2.0;
UISwipeGestureRecognizer *swipeDown = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeDown:)];
swipeDown.direction = UISwipeGestureRecognizerDirectionDown;
[colorController addGestureRecognizer:swipeDown];
return colorController;
}
#pragma mark - Dismiss
-(void)handleSwipeDown:(UISwipeGestureRecognizer*)swipeDownRec
{
[self dissmissColorControl];
}
#pragma mark - Color Control Expanded Animations
-(void)expandColorControl
{
[self makeExpandedView];
[self.superView addSubview:self.expandedView];
[UIView animateWithDuration:0.75 delay:0 usingSpringWithDamping:0.65 initialSpringVelocity:0.72 options:UIViewAnimationOptionCurveEaseInOut animations:^{
self.expandedView.alpha = 1.0;
self.expandedView.center = RectGetCenter(self.superView.bounds);
[self updateLabelColor];
} completion:^(BOOL finished) {
//
}];
}
-(void)dissmissColorControl
{
CGRect centerRect = CGRectMake(200, 400, 1.0, 1.0);
[UIView animateWithDuration:0.75 delay:0 usingSpringWithDamping:0.65 initialSpringVelocity:0.72 options:UIViewAnimationOptionCurveEaseInOut animations:^{
//
self.expandedView.frame = centerRect;
self.expandedView.alpha = 0.0;
} completion:^(BOOL finished) {
//
[self.expandedView removeFromSuperview];
}];
}
-(void)cleanUpAndRemove
{
}
/*
*/
@end