zjf
2023-03-06 392b76515f40376b6d36f40a114850ef63650384
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
//
//  PBDropdownMenu.m
//  IphoneBIMe
//
//  Created by zjf on 2018/8/8.
//  Copyright © 2018年 ProBIM. All rights reserved.
//
 
#import "PBDropdownMenu.h"
#import "PBIssueNavModel.h"
#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height
#define AnimateTime 0.25f   // 下拉动画时间
@interface PBDropdownMenu()<UIGestureRecognizerDelegate>
@property (nonatomic, strong) UIView *backgroundView;//背景视图
@property (nonatomic, strong) UITableView *tableView;//选择列表
@property (nonatomic, assign) BOOL isShow;//标识是否是弹出状态
@property (nonatomic, strong) NSMutableArray *selectIndexArr;//标识是否是弹出状态
@property (nonatomic, assign) NSInteger currentSelectIndex;//标识是否是弹出状态
@property (nonatomic, strong) NSMutableArray *buttonArr;//标识是否是弹出状态
@end
@implementation PBDropdownMenu {
    CGFloat _rowHeight;   // 下拉列表行高
    NSArray *_titleArr;
}
 
- (void)setBtnTitle:(NSString *)title {
    UIButton *clickBtn = self.buttonArr[self.currentSelectIndex];
    [clickBtn setTitle:title forState:UIControlStateNormal];
    CGFloat imageWidth = clickBtn.imageView.bounds.size.width;
    CGFloat labelWidth = clickBtn.titleLabel.bounds.size.width;
    clickBtn.imageEdgeInsets = UIEdgeInsetsMake(0, labelWidth, 0, -labelWidth);
    clickBtn.titleEdgeInsets = UIEdgeInsetsMake(0, -imageWidth, 0, imageWidth);
}
 
 
- (instancetype)initWithFrame:(CGRect)frame andArr:(NSArray *)arr {
    if (self = [super initWithFrame:frame]) {
        _titleArr = arr;
        [self creatMenuListWithArr:arr];
    }
    return self;
}
- (void)creatMenuListWithArr:(NSArray *)arr {
    _rowHeight = 40.f;
    CGFloat meunWidth = self.width / arr.count;
    for (NSInteger i = 0; i < arr.count; i++) {
        UIButton *clickBtn = [[UIButton alloc] init];
        clickBtn.backgroundColor = [UIColor z_colorWithR:242 G:242 B:242];
        [clickBtn setFrame:CGRectMake(meunWidth * i, 0, meunWidth, self.height)];
        clickBtn.titleLabel.font = [UIFont systemFontOfSize:TitleFontSize];
        [clickBtn setTitleColor:TitleColor forState:UIControlStateNormal];
        PBIssueNavModel *issueNavModel = arr[i][0];
        [clickBtn setImage:[UIImage imageNamed:@"Issue_ retrieve_default"] forState:UIControlStateNormal];
        [clickBtn setTitle:issueNavModel.ItemName forState:UIControlStateNormal];
        CGFloat imageWidth = clickBtn.imageView.bounds.size.width;
        CGFloat labelWidth = clickBtn.titleLabel.bounds.size.width;
        clickBtn.imageEdgeInsets = UIEdgeInsetsMake(0, labelWidth, 0, -labelWidth);
        clickBtn.titleEdgeInsets = UIEdgeInsetsMake(0, -imageWidth, 0, imageWidth);
        clickBtn.selected = NO;
        [clickBtn addTarget:self action:@selector(clickMainBtn:) forControlEvents:UIControlEventTouchUpInside];
        clickBtn.tag = 100 + i;
        [self.buttonArr addObject:clickBtn];
        [self addSubview:clickBtn];
    }
}
#pragma mark - 点击方法
- (void)clickMainBtn:(UIButton *)button{
    if (_currentSelectIndex == (button.tag - 100)) {
        if (_isShow) {
            [self hideDropDown];
        }else {
            [self showDropDown];
        }
    }else {
        if (_isShow) {
            [self hideDropDown];
        }else {
            self.currentSelectIndex = button.tag - 100;
            [self showDropDown];
        }
    }
}
#pragma mark - 显示下拉列表
- (void)showDropDown {
    [self.superview addSubview:self.backgroundView];
    [self.backgroundView addSubview:self.tableView];
    UIButton *clickBtn = self.buttonArr[self.currentSelectIndex];
    [UIView animateWithDuration:AnimateTime animations:^{
        clickBtn.imageView.transform = CGAffineTransformMakeRotation(M_PI);
        NSArray *titles = self->_titleArr[self.currentSelectIndex];
        CGFloat tableViewH = self->_rowHeight * titles.count;
        if (tableViewH > self->_rowHeight * 4) {
            tableViewH = self->_rowHeight * 4;
        }
        self->_tableView.frame = CGRectMake(0, 0, SCREEN_WIDTH, tableViewH);
        self.backgroundView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.3];
    }completion:^(BOOL finished) {
        self->_isShow = YES;
    }];
    [clickBtn setTitleColor:WarningColor forState:UIControlStateNormal];
    [clickBtn setImage:[UIImage imageNamed:@"Issue_ retrieve_show"] forState:UIControlStateNormal];
}
#pragma mark -  隐藏下拉列表
- (void)hideDropDown{
    UIButton *clickBtn = self.buttonArr[self.currentSelectIndex];
    [UIView animateWithDuration:AnimateTime animations:^{
        clickBtn.imageView.transform = CGAffineTransformIdentity;
        self.backgroundView.backgroundColor = [UIColor colorWithWhite:0 alpha:0];
        self->_tableView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 0);
    }completion:^(BOOL finished) {
        [self.backgroundView removeFromSuperview];
        [self.tableView removeFromSuperview];
        self->_isShow = NO;
    }];
    [clickBtn setTitleColor:TitleColor forState:UIControlStateNormal];
    [clickBtn setImage:[UIImage imageNamed:@"Issue_ retrieve_default"] forState:UIControlStateNormal];
}
 
#pragma mark - UITableView Delegate
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return _rowHeight;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray *titles = _titleArr[self.currentSelectIndex];
    return titles.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    NSArray *titles = _titleArr[self.currentSelectIndex];
    NSInteger selectIndex = [self.selectIndexArr[self.currentSelectIndex] integerValue];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        //---------------------------下拉选项样式,可在此处自定义-------------------------
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.textLabel.font          = [UIFont systemFontOfSize:16.f];
//        cell.textLabel.textColor     = [UIColor blackColor];
//        cell.selectionStyle          = UITableViewCellSelectionStyleNone;
        
        //---------------------------------------------------------------------------
    }
    if (selectIndex == indexPath.row) {
        cell.textLabel.textColor     = WarningColor;
        cell.selectionStyle          = UITableViewCellSelectionStyleBlue;
    }else {
        cell.textLabel.textColor     = TitleColor;
        cell.selectionStyle          = UITableViewCellSelectionStyleNone;
    }
    PBIssueNavModel *issueNavModel = titles[indexPath.row];
    cell.textLabel.text = issueNavModel.ItemName;
    return cell;
}
 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    self.selectIndexArr[self.currentSelectIndex] = [NSNumber numberWithInteger:indexPath.row];
    NSArray *titles = _titleArr[self.currentSelectIndex];
    PBIssueNavModel *issueNavModel = titles[indexPath.row];
    [self setBtnTitle:issueNavModel.ItemName];
    if ([self.delegate respondsToSelector:@selector(dropdownMenu:selectedCellNumber:)]) {
        [self.delegate dropdownMenu:self.buttonArr[self.currentSelectIndex] selectedCellNumber:indexPath.row];
    }
    [self hideDropDown];
}
 
- (UIView *)backgroundView {
    if (_backgroundView == nil) {
        _backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0 , self.y + self.height, SCREEN_WIDTH, SCREEN_HEIGHT - self.y - self.height)];
        _backgroundView.backgroundColor = [UIColor colorWithWhite:0 alpha:0];
        [_backgroundView setOpaque:NO];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(backTapped:)];
        tap.delegate = self;
        [_backgroundView addGestureRecognizer:tap];
    }
    return _backgroundView;
}
 
- (void)backTapped:(UITapGestureRecognizer *)sender {
    [self hideDropDown];
}
 
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"]) {
        return NO;
    } else {
        return YES;
    }
}
- (UITableView *)tableView {
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0,SCREEN_WIDTH,0)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.bounces = NO;
    return _tableView;
}
- (NSMutableArray *)selectIndexArr {
    if (_selectIndexArr == nil) {
        _selectIndexArr = [[NSMutableArray alloc] init];
        for (NSInteger i = 0; i < _titleArr.count; i++) {
            [_selectIndexArr addObject:[NSNumber numberWithInteger:0]];
        }
    }
    return _selectIndexArr;
}
- (NSMutableArray *)buttonArr {
    if (_buttonArr == nil) {
        _buttonArr = [[NSMutableArray alloc] init];
    }
    return _buttonArr;
}
 
@end