-
Notifications
You must be signed in to change notification settings - Fork 3
/
PullDownMenu.m
382 lines (339 loc) · 15 KB
/
PullDownMenu.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
//
// PullDownMenu.m
// Example
//
// Created by llwang on 13-11-18.
// Copyright (c) 2013年 Toursprung. All rights reserved.
//
#import "PullDownMenu.h"
#define kPullDownMenuAnimationDuration 0.3
#define TABLEVIEW_HEIGHT (iPhone5?300:250)
@interface PullDownMenu() <UITableViewDataSource, UITableViewDelegate>
{
NSArray *_dataArray;
NSDictionary *_selectedDetail;
BOOL _haveSub;
}
//整个界面容器View
@property (nonatomic, strong) UIView *currentMenuView;
//TableView的容器View
@property (strong, nonatomic) UIView *superViewOfTableView;
//filterView下显示PullDownMenu
@property (nonatomic, strong) UIView *filterView;
//点击消失menuView
@property (nonatomic, strong) UIButton *dismissButton;
//两个TableView
@property (nonatomic, strong) UITableView *mainTableView;
@property (nonatomic, strong) UITableView *subTableView;
@property (nonatomic, assign) NSInteger selectedMainRow;
@property (nonatomic, assign) NSInteger selectedMenuIndex;
- (void)fadeInCurrentMenu:(UIView *)menuView;
- (void)fadeOutMenu:(void (^)(BOOL finished))completion;
- (void)fadeOutMenu;
@end
@implementation PullDownMenu
//单例模式,获取PullDownMenu
+ (PullDownMenu *)sharedMenu
{
static PullDownMenu *sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [[self alloc] init];
});
return sharedInstance;
}
+ (void)showMenuBelowView:(UIView *)filterView
array:(NSArray *)array
selectedMenuIndex:(NSInteger)tag
selectedDetail:(NSDictionary*)selectedDetail
delegate:(id<PullDownMenuDelegate>)delegate
{
//创建下拉菜单的View
UIView *v = [[PullDownMenu sharedMenu] createPullDownMenuView:array//TableView显示数据
selectedMenuIndex:tag
selectedDetail:selectedDetail//选中Cell数据
delegate:delegate];
//设置下拉菜单的坐标
[PullDownMenu sharedMenu].filterView = filterView;
//显示View
[[PullDownMenu sharedMenu] prepareMenuToBeShown:v];
}
- (void)prepareMenuToBeShown:(UIView *)menuView
{
[[PullDownMenu sharedMenu] fadeInCurrentMenu:menuView];
}
+ (void)dismissActiveMenu:(void (^)(BOOL finished))completion
{
[[PullDownMenu sharedMenu] fadeOutMenu:completion];
}
+ (void)dismissActiveMenu
{
[[PullDownMenu sharedMenu] fadeOutMenu];
}
+ (BOOL)isMenuActive
{
return [PullDownMenu sharedMenu].currentMenuView != nil;
}
- (void)fadeOutMenu
{
[UIView animateWithDuration:kPullDownMenuAnimationDuration animations:^{
_currentMenuView.alpha = 0;
} completion:^(BOOL finished) {
if (_delegate && [_delegate respondsToSelector:@selector(pullDownMenuWillDismiss:)]) {
[_delegate pullDownMenuWillDismiss:self];
}
[_currentMenuView removeFromSuperview];
if (_delegate && [_delegate respondsToSelector:@selector(pullDownMenuDidDismiss:)]) {
[_delegate pullDownMenuDidDismiss:self];
}
_currentMenuView = nil;
}];
}
- (void)fadeOutMenu:(void (^)(BOOL finished))completion
{
if (_currentMenuView != nil) {
[_currentMenuView removeFromSuperview];
_currentMenuView = nil;
}
}
- (void)hideTableView:(void (^)(void))completion
{
[UIView animateWithDuration:0.0f
animations:^{
[_currentMenuView viewWithTag:999].frame = CGRectMake(0, -TABLEVIEW_HEIGHT, 320, TABLEVIEW_HEIGHT);
}
completion:^(BOOL finished){
completion();
}];
}
- (void)fadeInCurrentMenu:(UIView*)backView
{
//当前有下拉菜单,则先设置_currentMenuView = nil
if ([_currentMenuView viewWithTag:999] && [_currentMenuView viewWithTag:999].frame.origin.y == 44) {
[[PullDownMenu sharedMenu] hideTableView:^(){
[[PullDownMenu sharedMenu] fadeInCurrentMenu:backView];
}];
return;
}
_currentMenuView = backView;
__block CGFloat verticalOffset = _filterView.frame.origin.y + _filterView.frame.size.height;
[_filterView.superview insertSubview:backView belowSubview:_filterView];
//选中的选项的显示
[self selectRowForSelectedDetail];
//设置Frame
CGPoint toPoint = CGPointMake(backView.center.x, verticalOffset + CGRectGetHeight([backView viewWithTag:999].frame) / 2.0);
dispatch_block_t animationBlock = ^{
[backView viewWithTag:999].center = toPoint;
_dismissButton.alpha = 0.6f;
};
void(^completionBlock)(BOOL) = ^(BOOL finished) {
};
[UIView animateWithDuration:0.5
delay:0.0
options:UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionAllowUserInteraction
animations:animationBlock
completion:completionBlock];
}
- (UIView*)createPullDownMenuView:(NSArray *)array
selectedMenuIndex:(NSInteger)tag
selectedDetail:(NSDictionary*)selectedDetail
delegate:(id<PullDownMenuDelegate>)delegate
{
//整个view,包括dismisButton,TableView
UIView *containerView;
_dataArray = array;
_selectedMenuIndex = tag;
_selectedDetail = selectedDetail;
_delegate = delegate;
_haveSub = [[array lastObject] objectForKey:kPullDownChildren]==nil?NO:YES;
//已经显示PullDownMenu
if (_currentMenuView == nil) {
containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, [UIScreen mainScreen].bounds.size.height)];
containerView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
containerView.backgroundColor = [UIColor clearColor];//[[UIColor blackColor] colorWithAlphaComponent:0.5];
_dismissButton = [UIButton buttonWithType:UIButtonTypeCustom];
_dismissButton.frame = CGRectMake(0, 0, 320, [UIScreen mainScreen].bounds.size.height);
_dismissButton.backgroundColor = [UIColor blackColor];
_dismissButton.alpha = 0.0f;
[_dismissButton addTarget:self
action:@selector(fadeOutMenu)
forControlEvents:UIControlEventTouchUpInside];
[containerView addSubview:_dismissButton];
UIView *backgroundViewTemp = [[UIView alloc] initWithFrame:CGRectMake(0, -TABLEVIEW_HEIGHT, 320, TABLEVIEW_HEIGHT)];
backgroundViewTemp.tag = 999;
[containerView addSubview:backgroundViewTemp];
} else {
containerView = _currentMenuView;
}
UIView *backgroundView = [containerView viewWithTag:999];
backgroundView.backgroundColor = _haveSub?[UIColor cloudsColor]:[UIColor whiteColor];
if (_haveSub) {
[_mainTableView removeFromSuperview];
_mainTableView = nil;
_mainTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 120, TABLEVIEW_HEIGHT) style:UITableViewStylePlain];
_mainTableView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
_mainTableView.dataSource = self;
_mainTableView.delegate = self;
_mainTableView.showsHorizontalScrollIndicator = NO;
_mainTableView.showsVerticalScrollIndicator = NO;
_mainTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
_mainTableView.backgroundColor = [UIColor clearColor];
_mainTableView.backgroundView = nil;
[backgroundView addSubview:_mainTableView];
[_subTableView removeFromSuperview];
_subTableView = nil;
_subTableView = [[UITableView alloc] initWithFrame:CGRectMake(120, 0, 200, TABLEVIEW_HEIGHT) style:UITableViewStylePlain];
_subTableView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
_subTableView.dataSource = self;
_subTableView.delegate = self;
_subTableView.showsHorizontalScrollIndicator = NO;
_subTableView.showsVerticalScrollIndicator = NO;
_subTableView.backgroundColor = [UIColor whiteColor];
_subTableView.backgroundView = nil;
_subTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
[backgroundView addSubview:_subTableView];
} else {
[_mainTableView removeFromSuperview];
_mainTableView = nil;
_mainTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, TABLEVIEW_HEIGHT) style:UITableViewStylePlain];
_mainTableView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
_mainTableView.dataSource = self;
_mainTableView.delegate = self;
_mainTableView.showsHorizontalScrollIndicator = NO;
_mainTableView.showsVerticalScrollIndicator = NO;
_mainTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
_mainTableView.backgroundColor = [UIColor whiteColor];
_mainTableView.backgroundView = nil;
[backgroundView addSubview:_mainTableView];
}
return containerView;
}
/**
* 选中已选择的菜单项
*/
- (void)selectRowForSelectedDetail
{
if (_haveSub) {
if ([_selectedDetail isEqualToDictionary:@{}]) {
_selectedMainRow = 0;
[_mainTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]
animated:NO scrollPosition:UITableViewScrollPositionMiddle];
[_subTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]
animated:NO scrollPosition:UITableViewScrollPositionTop];
return;
}
for (int i=0; i<_dataArray.count; i++)
{
NSDictionary* mainDetail = [_dataArray objectAtIndex:i];
NSArray* children = [mainDetail objectForKey:kPullDownChildren];
BOOL isFoundSub = NO;
int j = 0;
for (NSDictionary* childDetail in children)
{
if ([_selectedDetail objectForKey:@"Title"] == nil) {
break;
}
if ([[childDetail objectForKey:@"Title"] isEqualToString:[_selectedDetail objectForKey:@"Title"]]) {
isFoundSub = YES;
break;
}
j++;
}
if (isFoundSub) {
_selectedMainRow = i;
[_mainTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]
animated:NO scrollPosition:UITableViewScrollPositionMiddle];
[_subTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:j inSection:0]
animated:NO scrollPosition:UITableViewScrollPositionTop];
break;
}
}
} else {
int i=0;
for (NSDictionary* detail in _dataArray)
{
if ([_selectedDetail objectForKey:@"Title"] == nil) {
break;
}
if ([[detail objectForKey:@"Title"] isEqualToString:[_selectedDetail objectForKey:@"Title"]]) {
[_mainTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]
animated:NO scrollPosition:UITableViewScrollPositionMiddle];
break;
}
i++;
}
[_mainTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]
animated:NO scrollPosition:UITableViewScrollPositionMiddle];
}
}
#pragma mark - TabelView Delegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == _mainTableView) {
return _dataArray.count;
} else {
return [[[_dataArray objectAtIndex:_selectedMainRow] objectForKey:kPullDownChildren]count];
}
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView == _mainTableView) {
cell.backgroundColor = _haveSub?[UIColor clearColor]:[UIColor whiteColor];
}
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView == _mainTableView) {
static NSString* mainCellIdentifier = @"mainCellIdentifier";
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:mainCellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:mainCellIdentifier];
if (_haveSub) {
[cell configureFlatCellWithColor:[UIColor clearColor] selectedColor:[UIColor whiteColor] position:FUICellBackgroundViewPositionSingle];
} else {
[cell configureFlatCellWithColor:[UIColor clearColor] selectedColor:[UIColor clearColor]
textColor:[UIColor blackColor] highlightTextColor:COLOR_THEME];
}
cell.textLabel.font = [UIFont systemFontOfSize:16];
}
cell.textLabel.text = [[_dataArray objectAtIndex:indexPath.row] objectForKey:kPullDownValue];
return cell;
} else {
static NSString* subCellIdentifier = @"subCellIdentifier";
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:subCellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:subCellIdentifier];
[cell configureFlatCellWithColor:[UIColor clearColor] selectedColor:[UIColor clearColor]
textColor:[UIColor blackColor] highlightTextColor:COLOR_THEME];
cell.textLabel.font = [UIFont systemFontOfSize:16];
}
cell.textLabel.text = [[[[_dataArray objectAtIndex:_selectedMainRow] objectForKey:kPullDownChildren]
objectAtIndex:indexPath.row] objectForKey:kPullDownValue];
return cell;
}
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (_delegate != nil && [_delegate respondsToSelector:@selector(pullDownMenu:didSelectedCell:selectedMenuIndex:)]) {
NSDictionary* info = nil;
if (tableView == _mainTableView) {
if (_haveSub) {
_selectedMainRow = indexPath.row;
[_subTableView reloadData];
return;
} else {
info = [_dataArray objectAtIndex:indexPath.row];
[_delegate pullDownMenu:nil didSelectedCell:info selectedMenuIndex:_selectedMenuIndex];
}
} else {
info = [[[_dataArray objectAtIndex:_selectedMainRow] objectForKey:kPullDownChildren] objectAtIndex:indexPath.row];
[_delegate pullDownMenu:nil didSelectedCell:info selectedMenuIndex:_selectedMenuIndex];
}
[[PullDownMenu sharedMenu] fadeOutMenu];
}
}
@end