最近做項目用到一個關(guān)于下拉列表選擇提現(xiàn)的控件,就在網(wǎng)上找了一下并結(jié)合自己的需求重新做了一個demo窖梁,希望用到的可以看看
首先介紹一下ZBDropdownMenu.h文件,該類有一個代理方法夹囚,用于點擊下拉列表后在列表消失或者出現(xiàn)時做一些事情纵刘,最后一個代理用于點擊獲取當(dāng)前cell,進行其他操作
@class ZBDropdownMenu;
@protocol ZBDropdownMenuDelegate <NSObject>
@optional
- (void)dropdownMenuWillShow:(ZBDropdownMenu *)menu; // 當(dāng)下拉菜單將要顯示時調(diào)用
- (void)dropdownMenuDidShow:(ZBDropdownMenu *)menu; // 當(dāng)下拉菜單已經(jīng)顯示時調(diào)用
- (void)dropdownMenuWillHidden:(ZBDropdownMenu *)menu; // 當(dāng)下拉菜單將要收起時調(diào)用
- (void)dropdownMenuDidHidden:(ZBDropdownMenu *)menu; // 當(dāng)下拉菜單已經(jīng)收起時調(diào)用
- (void)dropdownMenu:(ZBDropdownMenu *)menu selectedCellNumber:(NSInteger)number; // 當(dāng)選擇某個選項時調(diào)用
@end
@interface ZBDropdownMenu : UIView <UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong) UIButton * mainBtn; // 主按鈕 可以自定義樣式 可在.m文件中修改默認的一些屬性
@property (nonatomic,strong) NSString * title; // 可以定義按鈕title
@property (nonatomic, weak) id <ZBDropdownMenuDelegate>delegate;
- (void)setMenuTitles:(NSArray *)titlesArr rowHeight:(CGFloat)rowHeight; // 設(shè)置下拉菜單控件樣式
- (void)showDropDown; // 顯示下拉菜單
- (void)hideDropDown; // 隱藏下拉菜單
下面介紹一下最主要的兩個方法就是點擊消失或者出現(xiàn)下拉動畫荸哟,其實就是改變tableView的高度假哎,并加入動畫即可
- (void)showDropDown{ // 顯示下拉列表
[_listView.superview bringSubviewToFront:_listView]; // 將下拉列表置于最上層
if ([self.delegate respondsToSelector:@selector(dropdownMenuWillShow:)]) {
[self.delegate dropdownMenuWillShow:self]; // 將要顯示回調(diào)代理
}
// 改變箭頭方向
_arrowMark.transform = CGAffineTransformMakeRotation(M_PI);
[UIView animateWithDuration:AnimateTime animations:^{
_listView.frame = CGRectMake(_listView.X,
_listView.Y
, _listView.Witdh, _rowHeight *_titleArr.count);
_tableView.frame = CGRectMake(0, 0, _listView.Witdh, _listView.Height);
}completion:^(BOOL finished) {
if ([self.delegate respondsToSelector:@selector(dropdownMenuDidShow:)]) {
[self.delegate dropdownMenuDidShow:self]; // 已經(jīng)顯示回調(diào)代理
}
}];
_mainBtn.selected = YES;
}
隱藏動畫
- (void)hideDropDown{ // 隱藏下拉列表
if ([self.delegate respondsToSelector:@selector(dropdownMenuWillHidden:)]) {
[self.delegate dropdownMenuWillHidden:self]; // 將要隱藏回調(diào)代理
}
_arrowMark.transform = CGAffineTransformIdentity;
[UIView animateWithDuration:AnimateTime animations:^{
_listView.frame = CGRectMake(_listView.X, _listView.Y, _listView.Witdh, 0);
_tableView.frame = CGRectMake(0, 0, _listView.Witdh, _listView.Height);
}completion:^(BOOL finished) {
if ([self.delegate respondsToSelector:@selector(dropdownMenuDidHidden:)]) {
[self.delegate dropdownMenuDidHidden:self]; // 已經(jīng)隱藏回調(diào)代理
}
}];
_mainBtn.selected = NO;
}
使用方法很簡單,直接創(chuàng)建一個對象鞍历,并添加下拉列表的數(shù)據(jù),并給title設(shè)置自己的風(fēng)格
NSArray *titleArray = @[@"儲蓄銀行卡",@"微信",@"支付寶"];
ZBDropdownMenu * dropdownMenu = [[ZBDropdownMenu alloc] init];
[dropdownMenu setFrame:CGRectMake(14, 144, SCREEN_WIDTH - 28, 50)];
[dropdownMenu setMenuTitles:titleArray rowHeight:50];
dropdownMenu.delegate = self;
dropdownMenu.title = @"提現(xiàn)方式";
dropdownMenu.tag = 0;
// 給title設(shè)置富文本
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@: %@",dropdownMenu.title,titleArray[0]]];
[str addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0,dropdownMenu.title.length)];
[str addAttribute:NSForegroundColorAttributeName value:[UIColor lightGrayColor] range:NSMakeRange(dropdownMenu.title.length+4,[titleArray[0] length])];
[dropdownMenu.mainBtn setAttributedTitle:str forState:UIControlStateNormal];
[self.view addSubview:dropdownMenu];
詳細完整demo 地址下載