-
Notifications
You must be signed in to change notification settings - Fork 0
/
CCRadialMenuView.m
366 lines (291 loc) · 10.2 KB
/
CCRadialMenuView.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
//
// CCRadialMenu.m
// RadialEx
//
// Created by Charles Circlaeys on 29/01/2014.
// Copyright (c) 2014 Charles Circlaeys. All rights reserved.
//
#import "CCRadialMenuView.h"
const float kDegreeToRadians = M_PI / 180;
@interface CCRadialMenuView()
{
CGPoint _anchorPoint;
CGPoint _touchedAreaPoint;
NSDictionary *_valueDict;
NSArray *_collisionKeysArray;
NSMutableArray *_dataSourceArray;
CGFloat _dataSourceTotalWidth;
CGFloat _dataSourceTotalHeight;
CGSize _currentButtonSize;
}
@end
@implementation CCRadialMenuView
- (void)setup
{
_valueDict = [NSDictionary dictionaryWithObjectsAndKeys:@0, @"right", @90, @"bottom", @180, @"left", @270, @"top", nil];
_collisionKeysArray = [NSArray arrayWithObjects:@"right", @"bottom", @"left", @"top", nil];
}
- (id)init
{
self = [super init];
if (self) {
[self setup];
}
return self;
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setup];
}
return self;
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self];
NSInteger i = 0;
for (UIView *view in _dataSourceArray)
{
if (CGRectContainsPoint(view.frame, point))
{
[self.delegate radialMenuView:self didSelectViewAtIndex:i];
break;
}
++i;
}
}
- (void)updateFrame
{
CGFloat width = 0;
CGFloat height = 0;
for (UIView *view in _dataSourceArray)
{
width += view.frame.size.width;
height += view.frame.size.height;
width += _offset;
height += _offset;
}
_dataSourceTotalWidth = width;
_dataSourceTotalHeight = height;
_touchedAreaPoint = self.center;
self.frame = CGRectMake(self.center.x - width / 2, self.center.y - height / 2, width, height);
}
- (void)reloadData
{
NSInteger size = [self.dataSource numberOfButtonsForRadialMenuView:self];
_dataSourceArray = [[NSMutableArray alloc] initWithCapacity:size];
for (NSInteger i = 0; i < size; ++i)
{
[_dataSourceArray addObject:[self.dataSource radialMenuView:self buttonForIndex:i]];
}
_currentButtonSize = [self.delegate radialButtonSizeForRadialMenuView:self];
}
- (NSDictionary*)checkCollision
{
CGFloat globalRadius = _dataSourceTotalWidth / M_PI * (CGFloat)(180.f / 360.f);
if (_dataSourceArray.count == 1)
globalRadius += _currentButtonSize.width / 2;
NSMutableDictionary *collisionDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:@NO, @"right", @NO, @"bottom", @NO, @"left", @NO, @"top", nil];
CGFloat rightX = _touchedAreaPoint.x + globalRadius;
CGFloat leftX = _touchedAreaPoint.x - globalRadius;
CGFloat bottomY = _touchedAreaPoint.y + globalRadius;
CGFloat topY = _touchedAreaPoint.y - globalRadius;
if (rightX + _currentButtonSize.width > self.superview.bounds.size.width)
{
[collisionDict setObject:@YES forKey:@"right"];
}
else if (leftX - _currentButtonSize.width < self.superview.frame.origin.x)
{
[collisionDict setObject:@YES forKey:@"left"];
}
if (bottomY + _currentButtonSize.height > self.superview.bounds.size.height)
{
[collisionDict setObject:@YES forKey:@"bottom"];
}
else if (topY - _currentButtonSize.height < self.superview.frame.origin.y)
{
[collisionDict setObject:@YES forKey:@"top"];
}
return collisionDict;
}
- (BOOL)isCollisionForRadius:(CGFloat)radius withAngle:(NSInteger)angle
{
if (angle == 0 || angle == 360)
{
return (self.center.x + (radius + _currentButtonSize.width / 2) > self.superview.frame.size.width);
}
else if (angle == 90)
{
return (self.center.y + (radius + _currentButtonSize.height / 2) > self.superview.frame.size.height);
}
else if (angle == 180)
{
return (self.center.x - (radius + _currentButtonSize.width / 2) < self.superview.frame.origin.x);
}
else if (angle == 270)
{
return (self.center.y - (radius + _currentButtonSize.height / 2) < self.superview.frame.origin.y);
}
return NO;
}
- (NSString*)getStartFromCollisionDict:(NSDictionary*)collisionDict
{
for (NSInteger i = 0; i < 4; ++i)
{
NSString *key = [_collisionKeysArray objectAtIndex:i];
BOOL value = ((NSNumber*)[collisionDict objectForKey:key]).boolValue;
if (value)
{
i += 1;
while (TRUE)
{
if (i == 4)
i = 0;
NSString *key = [_collisionKeysArray objectAtIndex:i];
BOOL value = ((NSNumber*)[collisionDict objectForKey:key]).boolValue;
if (!value)
return key;
++i;
}
}
}
return nil;
}
- (NSString*)getEndFromCollisionDict:(NSDictionary*)collisionDict
{
for (NSInteger i = 0; i < 4; ++i)
{
NSString *key = [_collisionKeysArray objectAtIndex:i];
BOOL value = ((NSNumber*)[collisionDict objectForKey:key]).boolValue;
if (value)
{
i -= 1;
while (TRUE)
{
if (i < 0)
i = 3;
NSString *key = [_collisionKeysArray objectAtIndex:i];
BOOL value = ((NSNumber*)[collisionDict objectForKey:key]).boolValue;
if (!value)
return key;
--i;
}
}
}
return nil;
}
- (void)display
{
for (UIView *subview in self.subviews)
{
[subview removeFromSuperview];
}
if (!_dataSourceArray)
{
[self reloadData];
}
[self updateFrame];
NSDictionary *collisionDict = [self checkCollision];
NSString *startKey = [self getStartFromCollisionDict:collisionDict];
NSString *endKey = [self getEndFromCollisionDict:collisionDict];
NSInteger startAngle = 0;
if (startKey && ![startKey isEqualToString:@"right"])
startAngle = ((NSNumber*)[_valueDict objectForKey:startKey]).integerValue;
NSInteger endAngle = 360;
if (endKey && ![endKey isEqualToString:@"right"])
endAngle = ((NSNumber*)[_valueDict objectForKey:endKey]).integerValue;
NSInteger max = (startAngle > endAngle ? startAngle : endAngle);
NSInteger min = (startAngle < endAngle ? startAngle : endAngle);
NSInteger maxAngle = max - min;
if (maxAngle <= 0)
{
[self.delegate radialMenuView:self didFailToDisplayForError:CCRadialErrorSizeMax];
return;
}
if (maxAngle == 90)
{
self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width * 1.8f, self.frame.size.height * 1.8f);
}
CGFloat radius = _dataSourceTotalWidth / M_PI * (CGFloat)(180.f / (CGFloat)maxAngle);
if (maxAngle == 360)
radius += _currentButtonSize.width / 2;
if (_dataSourceArray.count == 1)
radius += _currentButtonSize.width / 2;
if (_touchedAreaPoint.x - _currentButtonSize.width / 2 < 0)
{
_touchedAreaPoint.x = _currentButtonSize.width / 2;
}
else if (_touchedAreaPoint.x > self.superview.bounds.size.width - _currentButtonSize.width / 2)
{
_touchedAreaPoint.x = self.superview.bounds.size.width - _currentButtonSize.width / 2;
}
if (_touchedAreaPoint.y - _currentButtonSize.height / 2 < 0)
{
_touchedAreaPoint.y = _currentButtonSize.height / 2;
}
else if (_touchedAreaPoint.y > self.superview.bounds.size.height - _currentButtonSize.height / 2)
{
_touchedAreaPoint.y = self.superview.bounds.size.height - _currentButtonSize.height / 2;
}
self.center = _touchedAreaPoint;
_anchorPoint = [self.superview convertPoint:_touchedAreaPoint toView:self];
if (maxAngle < 360 && ([self isCollisionForRadius:radius withAngle:startAngle] ||
[self isCollisionForRadius:radius withAngle:endAngle]))
{
[self.delegate radialMenuView:self didFailToDisplayForError:CCRadialErrorRadiusCollision];
if (!_shouldDisplayOnRadiusCollision)
return;
}
[self displayWithStart:startAngle maxAngle:maxAngle forCenter:_anchorPoint radius:radius];
if (_currentRadius != radius)
{
_currentRadius = radius;
[self.delegate radialMenuView:self didChangeRadius:_currentRadius];
}
if (!_backgroundView.superview)
[self insertSubview:_backgroundView atIndex:0];
_backgroundView.center = _anchorPoint;
}
- (void)displayWithStart:(NSInteger)startAngle maxAngle:(NSInteger)maxAngle forCenter:(CGPoint)center radius:(CGFloat)radius
{
NSInteger i = startAngle;
double offset = 0;
NSInteger count = (_dataSourceArray.count > 1 ? _dataSourceArray.count - 1 : 1);
if (maxAngle < 360)
offset = (maxAngle / count);
else
offset = maxAngle / _dataSourceArray.count;
NSInteger sourceIndex = 0;
for (NSInteger value = 0; value <= maxAngle && sourceIndex < _dataSourceArray.count; value += offset, i += offset, sourceIndex++)
{
if (i >= 360)
i = i - 360;
CGFloat degInRad = i * kDegreeToRadians;
CGFloat x = center.x + cos(degInRad) * radius;
CGFloat y = center.y + sin(degInRad) * radius;
UIView *view = [_dataSourceArray objectAtIndex:sourceIndex];
if (_animation == CCRadialAnimationFromCenter)
{
view.center = CGPointMake(self.bounds.size.width / 2, self.bounds.size.height / 2);
[UIView animateWithDuration:0.1f animations:^{
view.center = CGPointMake(x, y);
}];
}
else if (_animation == CCRadialAnimationNone)
{
view.center = CGPointMake(x, y);
}
[self addSubview:view];
}
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
@end