效果圖:
下拉菜單
首先昨登,上面下拉菜單為Button,點(diǎn)擊調(diào)用顯示方法贯底。第一行第二行......是一個tableView丰辣,下面是一個黑色背景,需要設(shè)置透明度丈甸。
最開始糯俗,tableView隱藏尿褪,背景View隱藏睦擂,調(diào)用顯示方法之后,背景出現(xiàn)杖玲,tableView出現(xiàn)顿仇。
因?yàn)橐蟹庋b性,所以新建一個DropDownView摆马,繼承于UIView.
DropDownView.h
#importtypedef void(^SelectBlock)(NSString *title, NSInteger row);
//@protocol DropDownViewDelegate
//-(void)didSelectRow:(NSInteger)row title:(NSString *)title;
//@end
@interface DropDownView : UIView@property (strong, nonatomic) UITableView *tableView;
/**
選項(xiàng)的數(shù)組
**/
@property (strong, nonatomic) NSArray *titles;
/**
行高
**/
@property (assign, nonatomic) CGFloat rowHeight;
/**
被選中的行,默認(rèn)0
**/
@property (assign, nonatomic) CGFloat selectIndex;//@property (weak, nonatomic) iddelegate;
/**
菜單寬度,默認(rèn)屏幕寬度
**/
@property (assign, nonatomic) CGFloat menuWidth;
/**
選中顏色
**/
@property (strong, nonatomic) UIColor *selectColor;
/**
正常顏色,默認(rèn)黑色
**/
@property (strong, nonatomic) UIColor *normalColor;
/**
觸發(fā)點(diǎn)擊的block
**/
@property (copy, nonatomic) SelectBlock selectRow;
/**
顯示的方法
**/
-(void)show;
/**
隱藏的方法
**/
-(void)hidden;
@end
DropDownView.m
#define WIDTH [UIScreen mainScreen].bounds.size.width
#import "DropDownView.h"
#import "DownViewTableViewCell.h"
static NSString *identifier = @"DownVIewCell";
@interface DropDownView()
@property (strong, nonatomic) UIView *bgView;//背景
@property (strong, nonatomic) UITapGestureRecognizer *tap;//手勢
@end
@implementation DropDownView
//初始化
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
//添加手勢臼闻,點(diǎn)擊調(diào)用隱藏方法
_tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hidden)];
if (_menuWidth == 0) {
_menuWidth = [UIScreen mainScreen].bounds.size.width;
}
//接下來是背景
_bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, _menuWidth, frame.size.height)];
_bgView.backgroundColor = [UIColor blackColor];
_bgView.alpha = 0;
[_bgView addGestureRecognizer:_tap];
[self addSubview:_bgView];
//關(guān)于tableView
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, _menuWidth, 0)];
_tableView.delegate =self;
_tableView.dataSource = self;
_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
[_tableView registerNib:[UINib nibWithNibName:@"DownViewTableViewCell" bundle:nil] forCellReuseIdentifier:identifier];
[self addSubview:_tableView];
//隱藏,0為NO囤采,1為YES
self.hidden = 1;
}
return self;
}
-(void)awakeFromNib{
[super awakeFromNib];
CGRect frame = self.frame;
_tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hidden)];
if (_menuWidth == 0) {
_menuWidth = [UIScreen mainScreen].bounds.size.width;
}
//接下來是背景
_bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, _menuWidth, frame.size.height)];
_bgView.backgroundColor = [UIColor blackColor];
_bgView.alpha = 0;
[_bgView addGestureRecognizer:_tap];
[self addSubview:_bgView];
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, _menuWidth, 0)];
_tableView.delegate =self;
_tableView.dataSource = self;
_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
[_tableView registerNib:[UINib nibWithNibName:@"DownViewTableViewCell" bundle:nil] forCellReuseIdentifier:identifier];
[self addSubview:_tableView];
self.hidden = 1;
}
-(void)show
{
if (!self.hidden) {
[self hidden];
return;
}
self.hidden = 0;
[UIView animateWithDuration:0.25 animations:^{
_bgView.alpha = 0.3;
_tableView.frame = CGRectMake(0, 0, _menuWidth, _titles.count * 36);
}];
}
-(void)hidden
{
[UIView animateWithDuration:0.25 animations:^{
_bgView.alpha = 0;
_tableView.frame = CGRectMake(0, 0, _menuWidth, 0);
} completion:^(BOOL finished) {
self.hidden = 1;
}];
}
//setter方法的增補(bǔ)
-(void)setTitles:(NSArray *)titles{
_titles = titles;
[_tableView reloadData];
}
-(UIColor *)selectColor{
if (_selectColor == nil) {
_selectColor = [UIColor colorWithRed:0x3e / 255.0 green:0x7e / 255.0 blue:0xc7 / 255.0 alpha:1];
}
return _selectColor;
}
-(UIColor *)normalColor{
if (_selectColor == nil) {
_selectColor = [UIColor blackColor];
}
return _selectColor;
}
-(CGFloat)rowHeight{
if (_rowHeight == 0) {
_rowHeight = 36;
}
return _rowHeight;
}
//表格代理
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return self.rowHeight;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return _titles.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
DownViewTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
cell.titleLabel.text = _titles[indexPath.row];
if (indexPath.row == _selectIndex) {
cell.titleLabel.textColor = self.selectColor;
cell.img.hidden = 0;
} else {
cell.titleLabel.textColor = self.normalColor;
cell.img.hidden = 1;
}
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
// [_delegate didSelectRow:indexPath.row title:_titles[indexPath.row]];
_selectRow(_titles[indexPath.row], indexPath.row);
_selectIndex = indexPath.row;
[_tableView reloadData];
[self hidden];
}
@end
新建一個tableViewCell述呐,設(shè)置cell的樣式。此處為左邊一個label蕉毯,右邊一個imageView乓搬。
最后在ViewController里面使用這個封裝的下拉菜單:
ViewController.m
#import "ViewController.h"
#import "DropDownView.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet DropDownView *dropView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_dropView.titles = @[@"第一行",@"第二行",@"第三行",@"第四行",@"第五行",@"第六行"];
_dropView.selectRow = ^(NSString *title, NSInteger row) {
NSLog(@"選中了第%d行,標(biāo)題是%@",(int)row,title);
};
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)open:(id)sender {
[_dropView show];
}
@end
ok,一個簡單的下拉菜單完畢代虾。