-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWAMenu.m
150 lines (119 loc) · 4.47 KB
/
WAMenu.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
//
// WAMenu.m
//
// Based on DIYMenu,
// Created by Jonathan Beilin on 8/13/12.
//
// Copyright (c) 2014 Mitchell Cooper.
// Copyright (c) 2012 DIY. All rights reserved.
//
#import "WAMenu.h"
#import "WAMenuItem.h"
@interface WAMenu ()
@property BOOL isActivated;
@property UIView *shadingView;
@end
@implementation WAMenu
#pragma mark - Init
- (void)setup {
CGRect frame = [UIScreen mainScreen].bounds;
self.frame = frame;
_menuItems = [NSMutableArray array];
self.clipsToBounds = true;
// Set up shadingview
_shadingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
self.shadingView.backgroundColor = [UIColor blackColor];
self.shadingView.alpha = 0.0f;
[self addSubview:self.shadingView];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tappedBackground)];
[self.shadingView addGestureRecognizer:tap];
}
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self setup];
}
return self;
}
- (id)init {
self = [super init];
if (self) [self setup];
return self;
}
#pragma mark - Show and Dismiss methods
- (void)showMenu {
NSEnumerator *frontToBackWindows = [[[UIApplication sharedApplication]windows]reverseObjectEnumerator];
for (UIWindow *window in frontToBackWindows) {
if (window.windowLevel == UIWindowLevelNormal) {
[window addSubview:self];
break;
}
}
//
// Animate in items & darken background
//
[self.menuItems enumerateObjectsUsingBlock:^(WAMenuItem *item, NSUInteger idx, BOOL *stop) {
item.transform = CGAffineTransformMakeTranslation(0, -ITEMHEIGHT * (idx + 2));
}];
[UIView animateWithDuration:0.22f delay:0.0f options:UIViewAnimationOptionCurveEaseOut animations:^{
[self.menuItems enumerateObjectsUsingBlock:^(WAMenuItem *item, NSUInteger idx, BOOL *stop) {
item.transform = CGAffineTransformMakeTranslation(0, 0);
}];
self.shadingView.alpha = 0.75f;
} completion:nil];
self.isActivated = true;
// Delegate call
if ([self.delegate respondsToSelector:@selector(menuActivated)]) {
[self.delegate performSelectorOnMainThread:@selector(menuActivated) withObject:nil waitUntilDone:false];
}
}
- (void)dismissMenu {
// Animate out the items
[UIView animateWithDuration:0.22f delay:0.0f options:UIViewAnimationOptionCurveEaseIn animations:^{
[self.menuItems enumerateObjectsUsingBlock:^(WAMenuItem *item, NSUInteger idx, BOOL *stop) {
item.transform = CGAffineTransformMakeTranslation(0, (CGFloat) -ITEMHEIGHT * (idx + 2));
}];
} completion:nil];
// Fade out the overlay window and remove self from it
[UIView animateWithDuration:0.22 delay:0.0f options:UIViewAnimationOptionCurveLinear animations:^{
self.shadingView.alpha = 0.0f;
} completion:^(BOOL finished) {
[self removeFromSuperview];
}];
self.isActivated = false;
}
#pragma mark - UI
- (void)tappedBackground {
[self dismissMenu];
if ([self.delegate respondsToSelector:@selector(menuCancelled)]) {
[self.delegate performSelectorOnMainThread:@selector(menuCancelled) withObject:nil waitUntilDone:false];
}
}
- (void)menuAction:(NSString *)action {
[self.delegate menuItemSelected:action];
[self dismissMenu];
}
#pragma mark - Item management
- (CGRect)newItemFrame {
UIApplication *application = [UIApplication sharedApplication];
float padding = application.statusBarHidden ? 0 :
MIN(application.statusBarFrame.size.height, application.statusBarFrame.size.width);
NSUInteger itemCount = [self.menuItems count];
return CGRectMake(0, padding + itemCount*ITEMHEIGHT, self.frame.size.height, ITEMHEIGHT);
}
- (void)addItem:(NSString *)name icon:(UIImage *)image color:(UIColor *)color font:(UIFont *)font {
WAMenuItem *item = [[WAMenuItem alloc] initWithFrame:[self newItemFrame]];
[item setName:name icon:image color:color font:font];
item.layer.shouldRasterize = true;
item.layer.rasterizationScale = [[UIScreen mainScreen] scale];
item.delegate = self;
[self.menuItems addObject:item];
[self addSubview:item];
}
- (void)clearMenu {
[self.menuItems enumerateObjectsUsingBlock:^(WAMenuItem *item, NSUInteger idx, BOOL *stop) {
[item removeFromSuperview];
}];
[self.menuItems removeAllObjects];
}
@end