美文网首页
封装下拉菜单

封装下拉菜单

作者: 兰歌er | 来源:发表于2017-11-27 16:02 被阅读29次

效果图:

A9CCF5C3D52B7B408E9175FFA03F213D.gif

下拉菜单
首先,上面下拉菜单为Button,点击调用显示方法。第一行第二行......是一个tableView,下面是一个黑色背景,需要设置透明度。

最开始,tableView隐藏,背景View隐藏,调用显示方法之后,背景出现,tableView出现。

因为要有封装性,所以新建一个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;

/**

选项的数组 

**/

@property (strong, nonatomic) NSArray *titles;

/**

行高

 **/

@property (assign, nonatomic) CGFloat rowHeight;

/**

被选中的行,默认0

 **/

@property (assign, nonatomic) CGFloat selectIndex;//@property (weak, nonatomic) iddelegate;

/**

菜单宽度,默认屏幕宽度

**/

@property (assign, nonatomic) CGFloat menuWidth;

/**

选中颜色

**/

@property (strong, nonatomic) UIColor *selectColor;

/**

正常颜色,默认黑色

**/

@property (strong, nonatomic) UIColor *normalColor;

/**

触发点击的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) {

//添加手势,点击调用隐藏方法

_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

_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方法的增补

-(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,设置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行,标题是%@",(int)row,title);

};

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

- (IBAction)open:(id)sender {

[_dropView show];

}

@end

ok,一个简单的下拉菜单完毕。

相关文章

  • iOS 下拉菜单封装

    效果如下:

  • 封装下拉菜单

    效果图: 下拉菜单首先,上面下拉菜单为Button,点击调用显示方法。第一行第二行......是一个tableVi...

  • iOS自定义一个带动画的下拉菜单

    **下拉菜单是非常常用的一个功能,这里封装了一个自带动画下拉菜单 **。 构建方法 根据传入的array来创建列表...

  • (四)iOS 实战项目开发:团购之下拉菜单的封装

    搜索城市结果的选择 封装下拉二级菜单的思路 封装下拉二级菜单的实现 完整的二级下拉菜单 搜索城市结果的选择 创建C...

  • 【JS】封装组件:DropDown(下拉菜单)

    第一次写js组件`(∩_∩)′,有什么不对的请随便指点出来, 单个下拉菜单组件 JS html 一次性生成多个下拉...

  • iOS仿微信、支付宝首页下拉菜单选择视图

    效果图展示、自动优化位置(上拉、下拉、左偏、右偏) 下拉菜单组件化封装的需求 项目开发初期、需求并不明确、没有统一...

  • iOS仿美团下拉菜单封装

    前言 这个控件比较常用,至少目前公司三个项目都用到了这个下拉菜单,是时候封装分享一下了。觉的不错的麻烦点个喜欢,三...

  • 第十九谈:下拉菜单

    本节课我们来开始学习 Bootstrap 的提供的下拉菜单组件。 一.下拉菜单 下拉菜单组件依赖于 Popper....

  • 简易下拉菜单

    今天做了一个简易的下拉菜单: 页面头部一个下拉菜单,鼠标滑过下拉菜单下方显示一个菜单项列表,且下拉菜单的背景图片切...

  • 本周工作总结

    本周主要做班群模块,班群信息的展示,学生信息,班群管理,查看作业本,查看作业报告,封装了查看作业本下拉菜单的展示,...

网友评论

      本文标题:封装下拉菜单

      本文链接:https://www.haomeiwen.com/subject/jdsdbxtx.html