-
Notifications
You must be signed in to change notification settings - Fork 0
/
RGBViewController.m
214 lines (175 loc) · 7.63 KB
/
RGBViewController.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
//
// RGBViewController.m
// W
//
// Created by liaa on 11/5/14.
// Copyright (c) 2014 kidliaa. All rights reserved.
//
#import "RGBViewController.h"
@interface RGBViewController ()
@property (nonatomic) UILabel *labelR;
@property (nonatomic) UILabel *labelG;
@property (nonatomic) UILabel *labelB;
@end
@implementation RGBViewController {
float h;
float lastPanX;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
UIPanGestureRecognizer *p = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
UITapGestureRecognizer *t = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(t:)];
[self.view addGestureRecognizer:t];
[self.view addGestureRecognizer:p];
lastPanX = -1;
[self uis];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:YES];
h = self.view.frame.size.height;
}
- (void) viewDidLayoutSubviews
{
h = self.view.frame.size.height;
}
- (void) viewWillLayoutSubviews
{
// self.view.frame = [UIScreen mainScreen].bounds;
}
- (void) uis
{
self.labelR = [[UILabel alloc] init];
self.labelR.translatesAutoresizingMaskIntoConstraints = NO;
self.labelR.textColor = [UIColor whiteColor];
self.labelG = [[UILabel alloc] init];
self.labelG.translatesAutoresizingMaskIntoConstraints = NO;
self.labelG.textColor = [UIColor whiteColor];
self.labelB = [[UILabel alloc] init];
self.labelB.translatesAutoresizingMaskIntoConstraints = NO;
self.labelB.textColor = [UIColor whiteColor];
self.labelR.text = [NSString stringWithFormat:@"%i",self.rValue];
self.labelG.text = [NSString stringWithFormat:@"%i",self.gValue];
self.labelB.text = [NSString stringWithFormat:@"%i",self.bValue];
[self.view addSubview:self.labelR];
[self.view addSubview:self.labelG];
[self.view addSubview:self.labelB];
NSDictionary *vD = @{@"l1": self.labelR};
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[l1(100)]"
options:0
metrics:nil
views:vD]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.labelR
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeBottom
multiplier:0.33
constant:0.0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.labelG
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeBottom
multiplier:0.66
constant:0.0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.labelG
attribute:NSLayoutAttributeLeft
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeLeft
multiplier:1
constant:0.0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.labelB
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:0.0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.labelB
attribute:NSLayoutAttributeLeft
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeLeft
multiplier:1
constant:0.0]];
}
- (void)t:(UITapGestureRecognizer *)recognizer
{
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)pan:(UIPanGestureRecognizer *)recognizer {
CGPoint translation = [recognizer translationInView:self.view];
CGPoint startPoint = [recognizer locationInView:self.view];
float y = startPoint.y;
if (lastPanX == -1) {
lastPanX = translation.x;
}
NSLog(@"y: %f | %f | %f", y, h/3, h*2/3);
if (0 <= y && y <= h/3) {
float old = [self.labelR.text floatValue];
float new = old + (translation.x - lastPanX);
lastPanX = translation.x;
int displayValue = floorf(new);
if (new >=0 && new <= 255) {
self.rValue = displayValue;
} else if ( new < 0) {
self.rValue = 0;
} else {
self.rValue = 255;
}
NSLog(@"r");
} else if ( y >= h*2/3) {
float old = [self.labelB.text floatValue];
float new = old + (translation.x - lastPanX);
lastPanX = translation.x;
int displayValue = floorf(new);
if (new >=0 && new <= 255) {
self.bValue = displayValue;
} else if ( new < 0) {
self.bValue = 0;
} else {
self.bValue = 255;
}
NSLog(@"b");
} else {
float old = [self.labelG.text floatValue];
float new = old + (translation.x - lastPanX);
lastPanX = translation.x;
int displayValue = floorf(new);
if (new >=0 && new <= 255) {
self.gValue = displayValue;
} else if ( new < 0) {
self.gValue = 0;
} else {
self.gValue = 255;
}
NSLog(@"g");
}
if (recognizer.state == UIGestureRecognizerStateEnded) {
lastPanX = 0.0;
} else {
[self update];
}
}
-(void)update
{
self.labelR.text = [NSString stringWithFormat:@"%i",self.rValue];
self.labelG.text = [NSString stringWithFormat:@"%i",self.gValue];
self.labelB.text = [NSString stringWithFormat:@"%i",self.bValue];
[self.delegate fullScreenValueChangedR:self.rValue g:self.gValue b:self.bValue];
}
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
lastPanX = 0.0;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
}
@end