-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDropMenuBar.m
More file actions
186 lines (151 loc) · 5.3 KB
/
Copy pathDropMenuBar.m
File metadata and controls
186 lines (151 loc) · 5.3 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
//
// DropMenuBar.m
// LinkageMenu
//
// Created by 魏小庄 on 2018/6/27.
// Copyright © 2018年 魏小庄. All rights reserved.
//
#import "DropMenuBar.h"
#import "DropMenuView.h"
#import "MenuAction.h"
#define kHeight [UIScreen mainScreen].bounds.size.height
@interface DropMenuBar ()<DropMenuViewDelegate>
@property (nonatomic, strong) NSMutableArray *menus;
@property (nonatomic, assign) CGRect orginFrame;
@property (nonatomic, assign) CGRect showFilterFrame;
@property (nonatomic, weak) MenuAction *currentAction;
@property (nonatomic, strong) UIView *bottomLine;
@property (nonatomic, weak) DropMenuView *showMenuView;
@end
@implementation DropMenuBar
- (instancetype)initWithAction:(NSArray <MenuAction *> *)actions {
if (self = [super init]) {
_actions = actions;
[self prepareUIWithItems];
}
return self;
}
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.layer.cornerRadius = 4;
/** 最下面横线 */
[self addSubview:self.bottomLine];
}
return self;
}
- (void)layoutSubviews {
[super layoutSubviews];
CGFloat width = self.frame.size.width / self.actions.count;
[self.actions enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
MenuAction *item = obj;
item.frame = CGRectMake(idx * width, 0, width, self.frame.size.height);
[item adjustFrame];
}];
if (!self.orginFrame.size.width) {
self.orginFrame = self.frame;
}
self.showFilterFrame = CGRectMake(0, self.frame.origin.y, [UIScreen mainScreen].bounds.size.width, self.frame.size.height);
self.bottomLine.frame = CGRectMake(0, self.frame.size.height - 0.8, self.frame.size.width, 0.8);
}
- (void)dealloc {
NSLog(@"%s", __func__);
}
#pragma mark - setUpUI
- (void)prepareUIWithItems {
__weak typeof(self) weakSelf = self;
[self.actions enumerateObjectsUsingBlock:^(MenuAction * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
[weakSelf addSubview:obj];
[obj addTarget:self action:@selector(actionDidClick:) forControlEvents:UIControlEventTouchUpInside];
void (^didDismissDropMenu)(void) = ^{
[weakSelf dismiss];
};
[obj setValue:didDismissDropMenu forKeyPath:@"didDismissDropMenu"];
DropMenuView *menuView = [[DropMenuView alloc]initWithAction:obj];
menuView.delegate = self;
[weakSelf.menus addObject:menuView];
}];
}
#pragma mark - public
- (void)setActions:(NSArray<MenuAction *> *)actions {
if (_actions.count)return;
_actions = actions;
[self prepareUIWithItems];
}
- (void)reloadMenus {
[self.showMenuView reloadList];
}
#pragma mark - 按钮点击推出菜单 (并且其他的菜单收起)
-(void)actionDidClick:(MenuAction *)action{
[self.actions enumerateObjectsUsingBlock:^(MenuAction * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if (![obj isEqual:action]) {
obj.selected = NO;
}
}];
self.currentAction = action;
NSUInteger index = [self.actions indexOfObject:action];
DropMenuView *showMenuView = self.menus[index];
// 将其他的菜单移除
[self.menus enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
DropMenuView *menuView = obj;
if (![menuView isEqual:showMenuView]) {
[menuView dismiss];
}
}];
self.showMenuView = showMenuView;
[showMenuView displayDropViewWithToobar:self];
}
- (void)adjustFrameWithShowDetail:(BOOL)show {
if (show) {
[UIView animateWithDuration:0.25 animations:^{
self.frame = self.showFilterFrame;
self.layer.cornerRadius = 0;
self.currentAction.selected = YES;
self.bottomLine.hidden = NO;
}];
}else {
[UIView animateWithDuration:0.25 animations:^{
self.frame = self.orginFrame;
self.layer.cornerRadius = 4;
self.currentAction.selected = NO;
self.bottomLine.hidden = YES;
}];
}
}
- (void)dismiss {
// 将其他的菜单移除
[self.menus enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
DropMenuView *menuView = obj;
[menuView dismiss];
}];
}
#pragma mark - 协议实现
- (void)dropMenuViewwillDisAppear:(DropMenuView *)view {
[self adjustFrameWithShowDetail:NO];
if ([self.delegate respondsToSelector:@selector(dropMenuViewWillDisAppear:selectAction:)]) {
[self.delegate dropMenuViewWillDisAppear:self selectAction:self.currentAction];
}
}
- (void)dropMenuViewwillAppear:(DropMenuView *)view {
[self adjustFrameWithShowDetail:YES];
if ([self.delegate respondsToSelector:@selector(dropMenuViewWillAppear:selectAction:)]) {
[self.delegate dropMenuViewWillAppear:self selectAction:self.currentAction];
}
}
#pragma mark - 懒加载
- (NSMutableArray *)menus {
if (!_menus) {
_menus = [NSMutableArray arrayWithCapacity:0];
}
return _menus;
}
- (UIView *)bottomLine {
if (!_bottomLine) {
_bottomLine = [[UIView alloc]init];
_bottomLine.backgroundColor = [UIColor colorWithWhite:0.85 alpha:0.8];
_bottomLine.hidden = YES;
}
return _bottomLine;
}
@end