-
Notifications
You must be signed in to change notification settings - Fork 9
/
EXSTOCircleMenuView.m
537 lines (471 loc) · 19.1 KB
/
EXSTOCircleMenuView.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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
//
// EXSTOCircleMenuView.m
//
#import "EXSTOCircleMenuView.h"
@interface EXSTOCircleMenuView ()
@property (nonatomic) NSMutableArray* buttons;
@property (unsafe_unretained, nonatomic) UIGestureRecognizer* recognizer;
@property (nonatomic) int hoverTag;
@property (nonatomic) UIColor* innerViewColor;
@property (nonatomic) UIColor* notifColor;
@property (nonatomic) UIColor* innerViewActiveColor;
@property (nonatomic) UIColor* borderViewColor;
@property (nonatomic) CGFloat radius;
@property (nonatomic) CGFloat maxAngle;
@property (nonatomic) CGFloat hoverScale;
@property (nonatomic) CGFloat animationDelay;
@property (nonatomic) CGFloat startingAngle;
@property (nonatomic) BOOL depth;
@property (nonatomic) CGFloat buttonRadius;
@property (nonatomic) CGFloat buttonBorderWidth;
@property (nonatomic, unsafe_unretained) UIView* clippingView;
@end
// each button is made up of two views (button image, and background view)
// the buttons get tagged, starting at 1
// components are identified by adding the corresponding offset to the button's logical tag
static int TAG_BUTTON_OFFSET = 100;
static int TAG_INNER_VIEW_OFFSET = 1000;
static CGPoint INITIALPOINT;
// constants used for the configuration dictionary
NSString* const CIRCLE_MENU_BUTTON_BACKGROUND_NORMAL = @"kCircleMenuNormal";
NSString* const CIRCLE_MENU_BUTTON_BACKGROUND_ACTIVE = @"kCircleMenuActive";
NSString* const CIRCLE_MENU_NOTIF_COLOR = @"kCircleMenuNotifColor";
NSString* const CIRCLE_MENU_BUTTON_BORDER = @"kCircleMenuBorder";
NSString* const CIRCLE_MENU_OPENING_DELAY = @"kCircleMenuDelay";
NSString* const CIRCLE_MENU_RADIUS = @"kCircleMenuRadius";
NSString* const CIRCLE_MENU_MAX_ANGLE = @"kCircleMenuMaxAngle";
NSString* const CIRCLE_MENU_HOVER_SCALE = @"kCircleMenuHoverScale";
NSString* const CIRCLE_MENU_DIRECTION = @"kCircleMenuDirection";
NSString* const CIRCLE_MENU_DEPTH = @"kCircleMenuDepth";
NSString* const CIRCLE_MENU_BUTTON_RADIUS = @"kCircleMenuButtonRadius";
NSString* const CIRCLE_MENU_BUTTON_BORDER_WIDTH = @"kCircleMenuButtonBorderWidth";
@implementation EXSTOCircleMenuView
- (id)initWithOptions:(NSDictionary*)anOptionsDictionary
{
self = [super init];
if (self) {
self.buttons = [NSMutableArray new];
if (anOptionsDictionary) {
[self updateWithOptions:anOptionsDictionary];
} else {
// using some default settings
self.innerViewColor = [UIColor colorWithRed:0.0 green:0.25 blue:0.5 alpha:1.0];
self.innerViewActiveColor = [UIColor colorWithRed:0.25 green:0.5 blue:0.75 alpha:1.0];
self.borderViewColor = [UIColor whiteColor];
self.notifColor = [UIColor clearColor];
self.animationDelay = 0.0;
self.radius = 65.0;
self.maxAngle = 180.0;
self.hoverScale = 1.2;
self.startingAngle = 0.0;
self.depth = NO;
self.buttonRadius = 39.0;
self.buttonBorderWidth = 2.0;
}
}
return self;
}
- (id)initAtOrigin:(CGPoint)aPoint usingOptions:(NSDictionary *)anOptionsDictionary withNotifArray:(NSMutableArray*)notif withImageArray:(NSArray *)anImageArray
{
self = [self initWithOptions:anOptionsDictionary];
if (self) {
self.frame = CGRectMake(aPoint.x - self.radius - self.buttonRadius, aPoint.y - self.radius - self.buttonRadius, self.radius * 2 + self.buttonRadius * 2, self.radius * 2 + self.buttonRadius * 2);
int tTag = 1;
int i = 0;
for (UIImage* img in anImageArray) {
UIView* tView;
if([[notif objectAtIndex:i] boolValue]){
tView = [self createButtonViewWithImage:img andTag:tTag withNotification: YES];
} else {
tView = [self createButtonViewWithImage:img andTag:tTag withNotification: NO];
}
[self.buttons addObject:tView];
tTag++;
i++;
}
}
return self;
}
- (id)initAtOrigin:(CGPoint)aPoint usingOptions:(NSDictionary *)anOptionsDictionary withNotifArray:(NSMutableArray*)notif withImages:(UIImage *)anImage, ...
{
self = [self initWithOptions:anOptionsDictionary];
if (self) {
self.frame = CGRectMake(aPoint.x - self.radius - self.buttonRadius, aPoint.y - self.radius - self.buttonRadius, self.radius * 2 + self.buttonRadius * 2, self.radius * 2 + self.buttonRadius * 2);
int tTag = 1;
va_list args;
va_start(args, anImage);
int i = 0;
for (UIImage* img = anImage; img != nil; img = va_arg(args, UIImage*)) {
UIView* tView;
if([[notif objectAtIndex:i] boolValue]){
tView = [self createButtonViewWithImage:img andTag:tTag withNotification: YES];
} else {
tView = [self createButtonViewWithImage:img andTag:tTag withNotification: NO];
}
[self.buttons addObject:tView];
tTag++;
i++;
}
va_end(args);
}
return self;
}
- (void)updateWithOptions:(NSDictionary *)anOptionsDictionary
{
self.innerViewColor = [anOptionsDictionary valueForKey:CIRCLE_MENU_BUTTON_BACKGROUND_NORMAL];
self.notifColor = [anOptionsDictionary valueForKey:CIRCLE_MENU_NOTIF_COLOR];
self.innerViewActiveColor = [anOptionsDictionary valueForKey:CIRCLE_MENU_BUTTON_BACKGROUND_ACTIVE];
self.borderViewColor = [anOptionsDictionary valueForKey:CIRCLE_MENU_BUTTON_BORDER];
self.animationDelay = [[anOptionsDictionary valueForKey:CIRCLE_MENU_OPENING_DELAY] doubleValue];
self.radius = [[anOptionsDictionary valueForKey:CIRCLE_MENU_RADIUS] doubleValue];
self.maxAngle = [[anOptionsDictionary valueForKey:CIRCLE_MENU_MAX_ANGLE] doubleValue];
self.hoverScale = [[anOptionsDictionary valueForKey:CIRCLE_MENU_HOVER_SCALE] doubleValue];
switch ([[anOptionsDictionary valueForKey:CIRCLE_MENU_DIRECTION] integerValue]) {
case EXSTOCircleMenuDirectionUp:
self.startingAngle = 0.0;
break;
case EXSTOCircleMenuDirectionRight:
self.startingAngle = 90.0;
break;
case EXSTOCircleMenuDirectionDown:
self.startingAngle = 180.0;
break;
case EXSTOCircleMenuDirectionLeft:
self.startingAngle = 270.0;
break;
}
self.depth = [[anOptionsDictionary valueForKey:CIRCLE_MENU_DEPTH] boolValue];
self.buttonRadius = [[anOptionsDictionary valueForKey:CIRCLE_MENU_BUTTON_RADIUS] doubleValue];
self.buttonBorderWidth = [[anOptionsDictionary valueForKey:CIRCLE_MENU_BUTTON_BORDER_WIDTH] doubleValue];
if (self.superview)
{
[self calculateButtonPositions];
}
}
/*!
* Convenience method that creates a circle button, consisting of
* the image, a background and a border.
* @param anImage image to be used as button's icon
* @param aTag unique identifier (should be index + 1)
* @return UIView to be used as button
*/
- (UIView*)createButtonViewWithImage:(UIImage*)anImage andTag:(int)aTag withNotification:(BOOL)hasNotif
{
UIButton* tButton = [UIButton buttonWithType:UIButtonTypeCustom];
CGFloat tButtonViewX = self.buttonRadius - anImage.size.width / 2;
CGFloat tButtonViewY = self.buttonRadius - anImage.size.height / 2;
tButton.frame = CGRectMake(tButtonViewX, tButtonViewY, anImage.size.width, anImage.size.height);
if(hasNotif){
tButton.layer.shadowColor = self.notifColor.CGColor;
tButton.layer.shadowRadius = 5.0f;
tButton.layer.shadowOpacity = .9;
tButton.layer.shadowOffset = CGSizeZero;
tButton.layer.masksToBounds = NO;
}
[tButton setImage:anImage forState:UIControlStateNormal];
tButton.tag = aTag + TAG_BUTTON_OFFSET;
UIView* tInnerView = [[EXSTORoundView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.buttonRadius * 2, self.buttonRadius * 2)];
tInnerView.backgroundColor = self.innerViewColor;
tInnerView.opaque = YES;
tInnerView.clipsToBounds = NO;
tInnerView.layer.cornerRadius = self.buttonRadius;
tInnerView.layer.borderColor = [self.borderViewColor CGColor];
tInnerView.layer.borderWidth = self.buttonBorderWidth;
if (self.depth) {
[self applyInactiveDepthToButtonView:tInnerView];
}
tInnerView.tag = aTag + TAG_INNER_VIEW_OFFSET;
[tInnerView addSubview:tButton];
return tInnerView;
}
/*!
* Does the math to put buttons on a circle.
*/
- (void)calculateButtonPositions
{
if (!self.clippingView) {
// climb view hierarchy up, until first view with clipToBounds = YES
self.clippingView = [self clippingViewOfChild:self];
}
CGFloat tMaxX = self.frame.size.width - self.buttonRadius;
CGFloat tMinX = self.buttonRadius;
CGFloat tMaxY = self.frame.size.height - self.buttonRadius;
CGFloat tMinY = self.buttonRadius;
if (self.clippingView) {
CGRect tClippingFrame = [self.clippingView convertRect:self.clippingView.bounds toView:self];
tMaxX = tClippingFrame.size.width + tClippingFrame.origin.x - self.buttonRadius * 2;
tMinX = tClippingFrame.origin.x;
tMaxY = tClippingFrame.size.height + tClippingFrame.origin.y - self.buttonRadius * 2;
tMinY = tClippingFrame.origin.y;
}
int tButtonCount = (int)self.buttons.count;
CGPoint tOrigin = CGPointMake(self.frame.size.width / 2, self.frame.size.height / 2);
CGFloat tRadius = self.radius;
int tCounter = 0;
// issue #1 - align buttons perfectly on circle of max angle is 360.0
if (self.maxAngle == 360.0) {
self.maxAngle = 360.0 - 360.0 / self.buttons.count;
}
for (UIView* tView in self.buttons) {
CGFloat tCurrentWinkel;
if (tCounter == 0) {
tCurrentWinkel = self.startingAngle + 0.0;
} else if (tCounter > 0 && tCounter < tButtonCount) {
tCurrentWinkel = self.startingAngle + (self.maxAngle / (tButtonCount - 1)) * tCounter;
} else {
tCurrentWinkel = self.startingAngle + self.maxAngle;
}
CGSize tSize = tView.frame.size;
CGFloat tX = tOrigin.x - (tRadius * cosf(tCurrentWinkel / 180.0 * M_PI)) - (tSize.width / 2);
CGFloat tY = tOrigin.y - (tRadius * sinf(tCurrentWinkel / 180.0 * M_PI)) - (tSize.width / 2);
if (tX > tMaxX) tX = tMaxX;
if (tX < tMinX) tX = tMinX;
if (tY > tMaxY) tY = tMaxY;
if (tY < tMinY) tY = tMinY;
CGRect tRect = CGRectMake(tX, tY, tSize.width, tSize.height);
tView.frame = tRect;
tCounter++;
}
}
/*!
* Climbs up the view hierarchy to find the first which has clipToBounds = YES.
* Returns the topmost view if no view has clipsToBound set to YES.
* @return UIView with clipToBounds = YES
*/
- (UIView*)clippingViewOfChild:(UIView*)aView
{
UIView* tView = [aView superview];
if (tView) {
if (tView.clipsToBounds) {
return tView;
} else {
return [self clippingViewOfChild:tView];
}
} else {
return aView;
}
}
- (void)openMenuWithRecognizer:(UIGestureRecognizer*)aRecognizer
{
self.recognizer = aRecognizer;
// use target action to get notified upon gesture changes
[aRecognizer addTarget:self action:@selector(gestureChanged:)];
CGPoint tOrigin = CGPointMake(self.frame.size.width / 2, self.frame.size.height / 2);
INITIALPOINT = tOrigin;
[self calculateButtonPositions];
for (UIView* tButtonView in self.buttons) {
[self addSubview:tButtonView];
tButtonView.alpha = 0.0;
CGFloat tDiffX = tOrigin.x - tButtonView.frame.origin.x - self.buttonRadius;
CGFloat tDiffY = tOrigin.y - tButtonView.frame.origin.y - self.buttonRadius;
tButtonView.transform = CGAffineTransformMakeTranslation(tDiffX, tDiffY);
}
CGFloat tDelay = 0.0;
for (UIView* tButtonView in self.buttons) {
tDelay = tDelay + self.animationDelay;
[UIView animateWithDuration:0.6 delay:tDelay usingSpringWithDamping:0.5 initialSpringVelocity:0.8 options:UIViewAnimationOptionCurveEaseOut animations:^{
tButtonView.alpha = 1.0;
tButtonView.transform = CGAffineTransformIdentity;
} completion:^(BOOL finished) {
}];
}
}
/*!
* Performs the closing animation.
*/
- (void)closeMenu
{
[self.recognizer removeTarget:self action:@selector(gestureChanged:)];
[UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
for (UIView* tButtonView in self.buttons) {
if (self.hoverTag > 0 && self.hoverTag == [self bareTagOfView:tButtonView]) {
tButtonView.transform = CGAffineTransformMakeScale(1.8, 1.8);
}
tButtonView.alpha = 0.0;
}
} completion:^(BOOL finished) {
[self removeFromSuperview];
if (self.delegate && [self.delegate respondsToSelector:@selector(circleMenuClosed)]) {
[self.delegate circleMenuClosed];
}
}];
}
- (void)gestureMovedToPoint:(CGPoint)aPoint
{
int tTag;
UIView* closestButton;
float shortestDist = 100000.0;
if([self distanceFromPoint: INITIALPOINT toPointB: aPoint] > self.buttonRadius)
{
for (UIView *but in self.buttons){
float distance = [self distanceFromPoint:but.center toPointB:aPoint];
if(distance < shortestDist)
{
shortestDist = distance;
closestButton = but;
}
}
tTag = [self bareTagOfView:closestButton];
}
else {
tTag = 0;
}
//UIView* tView = [self hitTest:aPoint withEvent:nil];
//int tTag = [self bareTagOfView:tView];
if (tTag > 0) {
if (tTag == self.hoverTag) {
// this button is already the active one
return;
}
else
{
// Animate previous button back to identity
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView animateWithDuration:0.1 animations:^
{
UIView* tInnerView = [self viewWithTag:self.hoverTag];
tInnerView.transform = CGAffineTransformIdentity;
[self sendSubviewToBack:tInnerView];
}];
}
self.hoverTag = tTag;
// display all (other) buttons in normal state
[self resetButtonState];
// display this button in active color
tTag = tTag + TAG_INNER_VIEW_OFFSET;
UIView* tInnerView = [self viewWithTag:tTag];
tInnerView.backgroundColor = self.innerViewActiveColor;
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView animateWithDuration:0.1 animations:^
{
[self bringSubviewToFront:tInnerView];
tInnerView.transform = CGAffineTransformMakeScale(self.hoverScale, self.hoverScale);
}];
if (self.depth) {
[self applyActiveDepthToButtonView:tInnerView];
}
} else {
// the view "hit" is none of the buttons -> display all in normal state
[self resetButtonState];
self.hoverTag = 0;
}
if ([self.delegate respondsToSelector:@selector(circleMenuHoverOnButtonWithIndex:)])
{
[self.delegate circleMenuHoverOnButtonWithIndex:tTag - 1 - TAG_INNER_VIEW_OFFSET];
}
}
- (void)resetButtonState
{
if (self.hoverTag > 0)
{
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView animateWithDuration:0.1 animations:^
{
UIView* tInnerView = [self viewWithTag:self.hoverTag];
tInnerView.transform = CGAffineTransformIdentity;
[self sendSubviewToBack:tInnerView];
}];
}
for (int i = 1; i <= self.buttons.count; i++) {
UIView* tView = [self viewWithTag:i + TAG_INNER_VIEW_OFFSET];
tView.backgroundColor = self.innerViewColor;
if (self.depth) {
[self applyInactiveDepthToButtonView:tView];
}
tView.transform = CGAffineTransformIdentity;
[self bringSubviewToFront:tView];
}
}
- (void)applyInactiveDepthToButtonView:(UIView*)aView
{
aView.layer.shadowColor = [[UIColor blackColor] CGColor];
aView.layer.shadowOffset = CGSizeMake(4,2);
aView.layer.shadowRadius = 8;
aView.layer.shadowOpacity = 0.35;
aView.layer.affineTransform = CGAffineTransformMakeScale(1.0, 1.0);
}
- (void)applyActiveDepthToButtonView:(UIView*)aView
{
aView.layer.shadowColor = [[UIColor blackColor] CGColor];
aView.layer.shadowOffset = CGSizeMake(2,1);
aView.layer.shadowRadius = 5;
aView.layer.shadowOpacity = 0.42;
aView.layer.affineTransform = CGAffineTransformMakeScale(0.985, 0.985);
}
/*!
* Target action method that gets called when the gesture used to open
* the EXSTOCircleMenuView changes.
*/
- (void)gestureChanged:(UILongPressGestureRecognizer*)sender
{
if (sender.state == UIGestureRecognizerStateChanged) {
CGPoint tPoint = [sender locationInView:self];
[self gestureMovedToPoint:tPoint];
} else if (sender.state == UIGestureRecognizerStateEnded) {
// determine wether a button was hit when the gesture ended
CGPoint tPoint = [sender locationInView:self];
UIView* closestButton;
float shortestDist = 100000.0;
int tTag;
if([self distanceFromPoint: INITIALPOINT toPointB: tPoint] > self.radius)
{
for (UIView *but in self.buttons){
float distance = [self distanceFromPoint:but.center toPointB:tPoint];
if(distance < shortestDist)
{
shortestDist = distance;
closestButton = but;
}
}
tTag = [self bareTagOfView:closestButton];
}
else {
tTag = 0;
}
//UIView* tView = [self hitTest:tPoint withEvent:nil];
//int tTag = [self bareTagOfView:closestButton];
if (tTag > 0) {
if (self.delegate && [self.delegate respondsToSelector:@selector(circleMenuActivatedButtonWithIndex:)]) {
[self.delegate circleMenuActivatedButtonWithIndex:tTag-1];
}
}
[self closeMenu];
}
}
/*!
* Return the 'virtual' tag of the button, no matter which of its components
* (image, background, border) is passed as argument.
* @param aView view to be examined
* @return 'virtual' tag without offsets
*/
- (int)bareTagOfView:(UIView*)aView
{
int tTag = (int)aView.tag;
if (tTag > 0) {
if (tTag >= TAG_INNER_VIEW_OFFSET) {
tTag = tTag - TAG_INNER_VIEW_OFFSET;
}
if (tTag >= TAG_BUTTON_OFFSET) {
tTag = tTag - TAG_BUTTON_OFFSET;
}
}
return tTag;
}
//line intersects rect
- (CGFloat)distanceFromPoint:(CGPoint)a toPointB:(CGPoint)b
{
return sqrt(pow(a.x - b.x, 2.0) + pow(a.y - b.y, 2.0));
}
@end
@implementation EXSTORoundView
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent*)event
{
// Pythagoras a^2 + b^2 = c^2
CGFloat tRadius = self.bounds.size.width / 2;
CGFloat tDiffX = tRadius - point.x;
CGFloat tDiffY = tRadius - point.y;
CGFloat tDistanceSquared = tDiffX * tDiffX + tDiffY * tDiffY;
CGFloat tRadiusSquared = tRadius * tRadius;
return tDistanceSquared < tRadiusSquared;
}
@end