-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathPFNavigationDropdownMenu.m
More file actions
285 lines (242 loc) · 9.97 KB
/
PFNavigationDropdownMenu.m
File metadata and controls
285 lines (242 loc) · 9.97 KB
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
//
// PFNavigationDropdownMenu.m
// PFNavigationDropdownMenu
//
// Created by Cee on 02/08/2015.
// Copyright (c) 2015 Cee. All rights reserved.
//
#import "PFNavigationDropdownMenu.h"
#import "PFTableView.h"
@interface PFNavigationDropdownMenu()
@property (nonatomic, strong) UIView *tableContainerView;
@property (nonatomic, strong) PFConfiguration *configuration;
@property (nonatomic, assign) CGRect mainScreenBounds;
@property (nonatomic, strong) UIButton *menuButton;
@property (nonatomic, strong) UILabel *menuTitle;
@property (nonatomic, strong) UIImageView *menuArrow;
@property (nonatomic, strong) UIView *backgroundView;
@property (nonatomic, strong) PFTableView *tableView;
@property (nonatomic, strong) NSArray *items;
@property (nonatomic, assign) BOOL isShown;
@property (nonatomic, assign) CGFloat navigationBarHeight;
@end
@implementation PFNavigationDropdownMenu
- (instancetype)initWithFrame:(CGRect)frame
title:(NSString *)title
items:(NSArray *)items
containerView:(UIView *)containerView
{
self = [super initWithFrame:frame];
if (self) {
// Init properties
self.configuration = [[PFConfiguration alloc] init];
self.tableContainerView = containerView;
self.navigationBarHeight = 44;
self.mainScreenBounds = [UIScreen mainScreen].bounds;
self.isShown = NO;
self.items = items;
// Init button as navigation title
self.menuButton = [[UIButton alloc] initWithFrame:frame];
[self.menuButton addTarget:self action:@selector(menuButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:self.menuButton];
self.menuTitle = [[UILabel alloc] initWithFrame:frame];
self.menuTitle.text = title;
self.menuTitle.textColor = [UINavigationBar appearance].titleTextAttributes[NSForegroundColorAttributeName];
self.menuTitle.textAlignment = NSTextAlignmentCenter;
self.menuTitle.font = self.configuration.cellTextLabelFont;
[self.menuButton addSubview:self.menuTitle];
self.menuArrow = [[UIImageView alloc] initWithImage:self.configuration.arrowImage];
[self.menuButton addSubview:self.menuArrow];
// Init table view
self.tableView = [[PFTableView alloc] initWithFrame:CGRectMake(self.mainScreenBounds.origin.x,
self.mainScreenBounds.origin.y,
self.mainScreenBounds.size.width,
self.mainScreenBounds.size.height + 300 - 64)
items:items
configuration:self.configuration];
__weak typeof(self) weakSelf = self;
self.tableView.selectRowAtIndexPathHandler = ^(NSUInteger indexPath){
__strong typeof(weakSelf) strongSelf = weakSelf;
if (strongSelf.didSelectItemAtIndexHandler)
{
strongSelf.didSelectItemAtIndexHandler(indexPath);
}
[strongSelf setMenuTitleText:items[indexPath]];
[strongSelf hideMenu];
strongSelf.isShown = NO;
[strongSelf layoutSubviews];
};
}
return self;
}
- (void)layoutSubviews
{
[super layoutSubviews];
[self.menuTitle sizeToFit];
self.menuTitle.center = CGPointMake(self.frame.size.width / 2.f, self.frame.size.height / 2.f);
[self.menuArrow sizeToFit];
self.menuArrow.center = CGPointMake(CGRectGetMaxX(self.menuTitle.frame) + self.configuration.arrowPadding, self.frame.size.height / 2.f);
}
- (void)showMenu
{
// Table view header
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 300)];
headerView.backgroundColor = self.configuration.cellBackgroundColor;
self.tableView.tableHeaderView = headerView;
// Reload data to dismiss highlight color of selected cell
[self.tableView reloadData];
// Init background view (under table view)
self.backgroundView = [[UIView alloc] initWithFrame:self.mainScreenBounds];
self.backgroundView.backgroundColor = self.configuration.maskBackgroundColor;
// Add background view & table view to container view
[self.tableContainerView addSubview:self.backgroundView];
[self.tableContainerView addSubview:self.tableView];
// Rotate arrow
[self rotateArrow];
// Change background alpha
self.backgroundView.alpha = 0;
// Animation
self.tableView.frame = CGRectMake(self.tableView.frame.origin.x,
-(CGFloat)(self.items.count) * self.configuration.cellHeight - 300,
self.tableView.frame.size.width,
self.tableView.frame.size.height);
[UIView animateWithDuration:self.configuration.animationDuration * 1.5f
delay:0
usingSpringWithDamping:.7
initialSpringVelocity:.5
options:0
animations:^{
self.tableView.frame = CGRectMake(self.tableView.frame.origin.x,
-300,
self.tableView.frame.size.width,
self.tableView.frame.size.height);
self.backgroundView.alpha = self.configuration.maskBackgroundOpacity;
}
completion:nil];
}
- (void)hideMenu
{
// Rotate arrow
[self rotateArrow];
// Change background alpha
self.backgroundView.alpha = self.configuration.maskBackgroundOpacity;
[UIView animateWithDuration:self.configuration.animationDuration * 1.5f
delay:0
usingSpringWithDamping:.7
initialSpringVelocity:.5
options:0
animations:^{
self.tableView.frame = CGRectMake(self.tableView.frame.origin.x,
-200,
self.tableView.frame.size.width,
self.tableView.frame.size.height);
self.backgroundView.alpha = self.configuration.maskBackgroundOpacity;
}
completion:nil];
[UIView animateWithDuration:self.configuration.animationDuration
delay:0
options:UIViewAnimationOptionTransitionNone
animations:^{
self.tableView.frame = CGRectMake(self.tableView.frame.origin.x,
-(CGFloat)(self.items.count) * self.configuration.cellHeight - 300,
self.tableView.frame.size.width,
self.tableView.frame.size.height);
self.backgroundView.alpha = 0;
} completion:^(BOOL finished) {
[self.tableView removeFromSuperview];
[self.backgroundView removeFromSuperview];
}];
}
- (void)rotateArrow
{
__weak typeof(self) weakSelf = self;
[UIView animateWithDuration:self.configuration.animationDuration
animations:^{
__strong typeof(weakSelf) strongSelf = weakSelf;
strongSelf.menuArrow.transform = CGAffineTransformRotate(strongSelf.menuArrow.transform, 180 * (CGFloat)(M_PI / 180));
}];
}
- (void)setMenuTitleText:(NSString *)title
{
self.menuTitle.text = title;
}
- (void)menuButtonTapped:(UIButton *)sender
{
self.isShown = !self.isShown;
if (self.isShown) {
[self showMenu];
} else {
[self hideMenu];
}
}
#pragma mark - Open/Close State
- (void)openMenu
{
self.isShown = YES;
[self showMenu];
}
- (void)closeMenu
{
self.isShown = NO;
[self hideMenu];
}
- (BOOL)isOpen
{
return [self isShown];
}
#pragma mark - Item Selection
- (void)selectItemAtIndex:(NSUInteger)index
{
if (index >= self.items.count) {
index = self.items.count - 1;
}
[self.tableView selectIndex:index];
}
#pragma mark - Setters
- (void)setCellHeight:(CGFloat)cellHeight
{
self.configuration.cellHeight = cellHeight;
}
- (void)setCellBackgroundColor:(UIColor *)cellBackgroundColor
{
self.configuration.cellBackgroundColor = cellBackgroundColor;
}
- (void)setCellTextLabelColor:(UIColor *)cellTextLabelColor
{
self.configuration.cellTextLabelColor = cellTextLabelColor;
}
- (void)setCellTextLabelFont:(UIFont *)cellTextLabelFont
{
self.configuration.cellTextLabelFont = cellTextLabelFont;
self.menuTitle.font = self.configuration.cellTextLabelFont;
}
- (void)setCellSelectionColor:(UIColor *)cellSelectionColor
{
self.configuration.cellSelectionColor = cellSelectionColor;
}
- (void)setCheckMarkImage:(UIImage *)checkMarkImage
{
self.configuration.checkMarkImage = checkMarkImage;
}
- (void)setAnimationDuration:(NSTimeInterval)animationDuration
{
self.configuration.animationDuration = animationDuration;
}
- (void)setArrowImage:(UIImage *)arrowImage
{
self.configuration.arrowImage = arrowImage;
self.menuArrow.image = self.configuration.arrowImage;
}
- (void)setArrowPadding:(CGFloat)arrowPadding
{
self.configuration.arrowPadding = arrowPadding;
}
- (void)setMaskBackgroundColor:(UIColor *)maskBackgroundColor
{
self.configuration.maskBackgroundColor = maskBackgroundColor;
}
- (void)setMaskBackgroundOpacity:(CGFloat)maskBackgroundOpacity
{
self.configuration.maskBackgroundOpacity = maskBackgroundOpacity;
}
@end