forked from domesticcatsoftware/MTQuadrantControl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTTTQuadrantControl.m
283 lines (227 loc) · 9.29 KB
/
TTTQuadrantControl.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
// TTTQuadrantControl.m
//
// Copyright (c) 2011 Mattt Thompson (http://mattt.me/)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#import <QuartzCore/QuartzCore.h>
#import "TTTQuadrantControl.h"
static NSUInteger const kAFTTTQuadrantNullLocation = 0;
@interface TTTQuadrantControl ()
@property (readwrite, nonatomic, assign) TTTQuadrantLocation activeLocation;
@property (readwrite, nonatomic, retain) TTTQuadrantView *topLeftQuadrantView;
@property (readwrite, nonatomic, retain) TTTQuadrantView *topRightQuadrantView;
@property (readwrite, nonatomic, retain) TTTQuadrantView *bottomLeftQuadrantView;
@property (readwrite, nonatomic, retain) TTTQuadrantView *bottomRightQuadrantView;
- (void)commonInit;
@end
@implementation TTTQuadrantControl
@synthesize delegate = _delegate;
@synthesize activeLocation = _activeLocation;
@synthesize topLeftQuadrantView = _topLeftQuadrantView;
@synthesize topRightQuadrantView = _topRightQuadrantView;
@synthesize bottomLeftQuadrantView = _bottomLeftQuadrantView;
@synthesize bottomRightQuadrantView = _bottomRightQuadrantView;
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (!self) {
return nil;
}
[self commonInit];
return self;
}
- (id)initWithCoder:(NSCoder *)coder {
self = [super initWithCoder:coder];
if (!self) {
return nil;
}
[self commonInit];
return self;
}
- (void)commonInit {
self.topRightQuadrantView = [[[TTTQuadrantView alloc] initWithFrame:CGRectZero] autorelease];
[self addSubview:self.topRightQuadrantView];
self.topLeftQuadrantView = [[[TTTQuadrantView alloc] initWithFrame:CGRectZero] autorelease];
[self addSubview:self.topLeftQuadrantView];
self.bottomLeftQuadrantView = [[[TTTQuadrantView alloc] initWithFrame:CGRectZero] autorelease];
[self addSubview:self.bottomLeftQuadrantView];
self.bottomRightQuadrantView = [[[TTTQuadrantView alloc] initWithFrame:CGRectZero] autorelease];
[self addSubview:self.bottomRightQuadrantView];
self.layer.cornerRadius = 8.0f;
self.layer.borderWidth = 1.0f;
self.layer.borderColor = [[UIColor lightGrayColor] CGColor];
self.layer.masksToBounds = YES;
}
- (void)dealloc {
[_topLeftQuadrantView release];
[_topRightQuadrantView release];
[_bottomLeftQuadrantView release];
[_bottomRightQuadrantView release];
[super dealloc];
}
- (void)setNumber:(NSNumber *)number
caption:(NSString *)caption
action:(SEL)action
forLocation:(TTTQuadrantLocation)location
{
TTTQuadrantView * quadrantView = [self quadrantViewAtLocation:location];
quadrantView.number = number;
quadrantView.caption = caption;
quadrantView.action = action;
}
- (TTTQuadrantLocation)locationAtPoint:(CGPoint)point {
if (point.x < self.center.x) {
if (point.y < self.center.y) {
return TopLeftLocation;
} else {
return BottomLeftLocation;
}
} else {
if (point.y < self.center.y) {
return TopRightLocation;
} else {
return BottomRightLocation;
}
}
}
- (TTTQuadrantView *)quadrantViewAtLocation:(TTTQuadrantLocation)location {
switch (location) {
case TopRightLocation:
return self.topRightQuadrantView;
case TopLeftLocation:
return self.topLeftQuadrantView;
case BottomLeftLocation:
return self.bottomLeftQuadrantView;
case BottomRightLocation:
return self.bottomRightQuadrantView;
default:
return nil;
}
}
#pragma mark - UIResponder
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self];
self.activeLocation = [self locationAtPoint:point];
[self setNeedsDisplay];
[[self subviews] makeObjectsPerformSelector:@selector(setNeedsDisplay)];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
switch (self.activeLocation) {
case TopLeftLocation:
case TopRightLocation:
case BottomLeftLocation:
case BottomRightLocation:
[self.delegate performSelector:[[self quadrantViewAtLocation:self.activeLocation] action]];
default:
break;
}
self.activeLocation = kAFTTTQuadrantNullLocation;
[self setNeedsDisplay];
[[self subviews] makeObjectsPerformSelector:@selector(setNeedsDisplay)];
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
self.activeLocation = kAFTTTQuadrantNullLocation;
[self setNeedsDisplay];
[[self subviews] makeObjectsPerformSelector:@selector(setNeedsDisplay)];
}
#pragma mark - UIView
- (void)layoutSubviews {
CGFloat width = round(self.frame.size.width / 2.0);
CGFloat height = round(self.frame.size.height / 2.0);
self.topLeftQuadrantView.frame = CGRectMake(0.0f, 0.0f, width, height);
self.topRightQuadrantView.frame = CGRectMake(width, 0.0f, width, height);
self.bottomLeftQuadrantView.frame = CGRectMake(0.0f, height, width, height);
self.bottomRightQuadrantView.frame = CGRectMake(width, height, width, height);
}
- (void)drawRect:(CGRect)rect {
CGContextRef c = UIGraphicsGetCurrentContext();
// Background Fill
CGContextSetFillColorWithColor(c, [[UIColor whiteColor] CGColor]);
CGContextFillRect(c, rect);
// Vertical Divider
CGContextMoveToPoint(c, round(CGRectGetMidX(rect)) + 0.5f, 0.0f);
CGContextAddLineToPoint(c, round(CGRectGetMidX(rect)) + 0.5f, round(rect.size.height));
// Horizontal Divider
CGContextMoveToPoint(c, 0.0f, round(CGRectGetMidY(rect)) + 0.5f);
CGContextAddLineToPoint(c, round(rect.size.width), round(CGRectGetMidY(rect)) + 0.5f);
CGContextSetLineWidth(c, 0.5f);
CGContextSetStrokeColorWithColor(c, [[UIColor lightGrayColor] CGColor]);
CGContextDrawPath(c, kCGPathStroke);
[self.topLeftQuadrantView setHighlighted:NO];
[self.topRightQuadrantView setHighlighted:NO];
[self.bottomLeftQuadrantView setHighlighted:NO];
[self.bottomRightQuadrantView setHighlighted:NO];
// Draw gradient background for selected quadrant
if (self.activeLocation) {
TTTQuadrantView *activeQuadrantView = [self quadrantViewAtLocation:self.activeLocation];
activeQuadrantView.highlighted = YES;
CGRect activeRect = activeQuadrantView.frame;
size_t num_locations = 2;
CGFloat locations[2] = {0.0, 1.0};
CGFloat components[8] = {0.000, 0.459, 0.968, 1.000, // #0075F6
0.000, 0.265, 0.897, 1.000}; // #0043E4
CGColorSpaceRef rgbColorspace = CGColorSpaceCreateDeviceRGB();
CGGradientRef gradient = CGGradientCreateWithColorComponents(rgbColorspace, components, locations, num_locations);
CGContextClipToRect(c, *(CGRect *)&activeRect);
CGContextDrawLinearGradient(c, gradient,
CGPointMake(CGRectGetMidX(activeRect), CGRectGetMinY(activeRect)),
CGPointMake(CGRectGetMidX(activeRect), CGRectGetMaxY(activeRect)), 0);
CGColorSpaceRelease(rgbColorspace);
CGGradientRelease(gradient);
}
}
@end
#pragma mark -
@implementation TTTQuadrantView
@synthesize number = _number;
@synthesize caption = _caption;
@synthesize highlighted;
@synthesize action;
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (!self) {
return nil;
}
self.highlighted = NO;
self.opaque = NO;
return self;
}
- (void)dealloc {
[_number release];
[_caption release];
[super dealloc];
}
#pragma mark - UIView
- (void)drawRect:(CGRect)rect {
static NSNumberFormatter *_numberFormatter;
if (!_numberFormatter) {
_numberFormatter = [[NSNumberFormatter alloc] init];
[_numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
}
[(self.highlighted ? [UIColor whiteColor] : [UIColor blackColor]) set];
NSString * numberString = [_numberFormatter stringFromNumber:self.number];
CGSize numberTextSize = [numberString sizeWithFont:[UIFont boldSystemFontOfSize:22] constrainedToSize:self.bounds.size];
CGPoint numberDrawPoint = CGPointMake(round((self.bounds.size.width - numberTextSize.width) / 2.0), 3.0f);
[numberString drawAtPoint:numberDrawPoint withFont:[UIFont boldSystemFontOfSize:22]];
[(self.highlighted ? [UIColor whiteColor] : [UIColor darkGrayColor]) set];
CGSize captionTextSize = [self.caption sizeWithFont:[UIFont boldSystemFontOfSize:12] constrainedToSize:self.bounds.size];
CGPoint captionDrawPoint = CGPointMake(round((self.bounds.size.width - captionTextSize.width) / 2.0), 27.0f);
[self.caption drawAtPoint:captionDrawPoint withFont:[UIFont boldSystemFontOfSize:12]];
}
@end