最近由于公司在开发私有库组件,给我分配了这个下降菜单栏的组件,恰好现在任务完成了,有时间。就把自己写的东西,分享一下。
我把这个功能封装成了一个framework,使用的时候只要一句话就可以实现了
用法:
NSArray *titleArr = @[@"发起群聊",@"添加好友",@"扫一扫",@"收付款",@"帮助与反馈"];
//类方法调用 属性只能使用默认值
[YHDropMenuView showDropMenuFrame:CGRectMake(108, 200, 120, 208) ArrowOffset:32.f TitleArr:titleArr ImageArray:nil RowHeight:40 Delegate:self ];
//初始化方法调用
YHDropMenuView *dropMenuView = [[YHDropMenuView alloc] initWithFrame:CGRectMake(100, 460, 150, 208) ArrowOffset:16.f TitleArr:titleArr ImageArray:imageArr RowHeight:40.0 Delegate:self];
dropMenuView.arrowColor = @"97d200"; //改变背景颜色
dropMenuView.titleColor = [UIColor yellowColor]; //改变字体颜色
dropMenuView.lineColor = [UIColor redColor]; //改变线条颜色
下面是主要文件的生命与实现 ,代码简单,逻辑清楚
YHDropMenuView.h文件
#import@protocol YHDropMenuViewDelegate
//代理方法 回调
-(void) didSelectRowAtIndex:(NSInteger)index Title:(NSString *) title Image:(NSString *) image;
@end
@interface YHDropMenuView : UIView
/** * 显示下拉菜单
* frame 显示的位置以及大小
* arrowOffset 箭头的x偏移值
* titleArr 标题数组
* imageArr 如果不需要显示图片可穿空
* rowHeight 每一行的高度 * /
//通过类方法调用
+(instancetype) showDropMenuFrame:(CGRect) frame ArrowOffset:(CGFloat) arrowOffset TitleArr:(NSArray *) titleArray ImageArray:(NSArray *) imageArray RowHeight:(CGFloat)rowHeight Delegate:(id) delegate; //通过初始化方法调用
-(instancetype) initWithFrame:(CGRect)frame ArrowOffset:(CGFloat)arrowOffset TitleArr:(NSArray *)titleArray ImageArray:(NSArray *)imageArray RowHeight:(CGFloat)rowHeight Delegate:(id)delegate;// 移除menuView-(void) removeYHDropMenuView; // 文字颜色 默认为微信颜色
@property (nonatomic,strong) UIColor *titleColor;
// 线条颜色 默认为微信颜色
@property (nonatomic, strong) UIColor *lineColor;
// 箭头x偏移值
@property (nonatomic, assign) CGFloat arrowOffset;
// 箭头的颜色 (背景颜色 默认为微信颜色) (传16进制类型字符串 例如:#97d216)
@property (nonatomic, copy) NSString *arrowColor;
// 代理
@property (nonatomic, weak) iddelegate;
@end
YHDropMenuView.m文件 实现部分
#import "YHDropMenuView.h"
#import "YHDropMenuCell.h"
#import "YHDropMenuModel.h"
#define kWindow [UIApplication sharedApplication].windows.firstObject
#define kCellIdentifier @"cellIdentifier"
@interface YHDropMenuView()
//蒙版
@property (nonatomic, strong) UIView *cover;
// tableView
@property (nonatomic, strong) UITableView *tableView;
// 存放标题和图片数组
@property (nonatomic, strong) NSMutableArray *titleImageArrM;
// rowHeight
@property (nonatomic, assign) CGFloat rowHeight;
// rgb的可变数组
@property (nonatomic, strong) NSMutableArray *RGBStrValueArr;
@end
@implementation YHDropMenuView
//类方法加载
+(instancetype) showDropMenuFrame:(CGRect)frame ArrowOffset:(CGFloat)arrowOffset TitleArr:(NSArray *)titleArray ImageArray:(NSArray *)imageArray RowHeight:(CGFloat)rowHeight Delegate:(id)delegate{ return [[self alloc] initWithFrame:frame ArrowOffset:arrowOffset TitleArr:titleArray ImageArray:imageArray RowHeight:rowHeight Delegate:delegate]; }
//初始化方法加载
-(instancetype) initWithFrame:(CGRect)frame ArrowOffset:(CGFloat)arrowOffset TitleArr:(NSArray *)titleArray ImageArray:(NSArray *)imageArray RowHeight:(CGFloat)rowHeight Delegate:(id)delegate{
if (self = [super initWithFrame:frame]) {
//初始化frame
self.frame = frame;
_arrowOffset = arrowOffset;
_rowHeight = rowHeight;
_delegate = delegate;
//默认值
self.RGBStrValueArr = [NSMutableArray arrayWithObjects:@(54),@(54),@(54), nil];
_titleColor = [UIColor whiteColor];
_lineColor = [UIColor whiteColor];
}
for(int i = 0;i<titleArray.count;i++){
NSMutableDictionary *tempDic = [NSMutableDictionary dictionary];
if (titleArray.count != 0) {
[tempDic setObject:titleArray[i] forKey:@"title"];
}
if (imageArray.count != 0) {
[tempDic setObject:imageArray[i] forKey:@"image"];
}
YHDropMenuModel *model = [YHDropMenuModel dropMenuWithDictonary:tempDic];
[self.titleImageArrM addObject:model];
}
[kWindow addSubview:self.cover];
UITapGestureRecognizer *tapCover = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapCoverClick)];
[self.cover addGestureRecognizer:tapCover];
self.backgroundColor = [UIColor clearColor];
[kWindow addSubview:self];
[self addSubview:self.tableView];
return self;
}
#pragma mark - UITableViewDelegate
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.titleImageArrM.count;
}
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
YHDropMenuCell *cell = [YHDropMenuCell dropMenuCellWithTableView:tableView];
cell.model = self.titleImageArrM[indexPath.row];
float r = [self.RGBStrValueArr[0] floatValue] / 255.0;
float g = [self.RGBStrValueArr[1] floatValue] / 255.0;
float b = [self.RGBStrValueArr[2] floatValue] / 255.0;
cell.backgroundColor = [UIColor colorWithRed:r green:g blue:b alpha:1];
cell.titleL.textColor = _titleColor;
cell.line1.backgroundColor = _lineColor;
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
YHDropMenuModel *model = self.titleImageArrM[indexPath.row];
if ([_delegate respondsToSelector:@selector(didSelectRowAtIndex:Title:Image:)]) {
[_delegate didSelectRowAtIndex:indexPath.row Title:model.title Image:model.image];
}
[self removeYHDropMenuView];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return _rowHeight;
}
#pragma mark - 蒙版点击
- (void)tapCoverClick {
[self removeYHDropMenuView];
}
#pragma mark - 移除蒙版
-(void) removeYHDropMenuView{
[self.tableView removeFromSuperview];
[self.cover removeFromSuperview];
[self removeFromSuperview];
}
#pragma mark - 懒加载 Getter
- (UIView *)cover {
if (!_cover) {
_cover = [[UIView alloc] initWithFrame:kWindow.bounds];
_cover.backgroundColor = [UIColor clearColor];
}
return _cover;
}
- (UITableView *)tableView {
if (!_tableView) {
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 8, self.frame.size.width, self.frame.size.height - 8) style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.tableFooterView = [UIView new];
_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
_tableView.scrollEnabled = NO;
_tableView.layer.masksToBounds = YES;
_tableView.layer.cornerRadius = 6.f;
}
return _tableView;
}
- (NSMutableArray *)titleImageArrM {
if (!_titleImageArrM) {
_titleImageArrM = [NSMutableArray array];
}
return _titleImageArrM;
}
- (NSMutableArray *)RGBStrValueArr {
if (!_RGBStrValueArr) {
_RGBStrValueArr = [NSMutableArray array];
}
return _RGBStrValueArr;
}
#pragma mark - Setter方法
//箭头允许的偏移量
- (void)setArrowOffset:(CGFloat)arrowOffset {
_arrowOffset = arrowOffset;
[self setNeedsDisplay];
}
//箭头颜色 传16进制
- (void)setArrowColor:(NSString *)arrowColor {
_arrowColor = arrowColor;
self.RGBStrValueArr = [self hexStringToColor:arrowColor];
[self setNeedsDisplay];
}
//文字颜色
- (void)setTitleColor:(UIColor *)titleColor {
_titleColor = titleColor;
[self.tableView reloadData];
}
//线条颜色
- (void)setLineColor:(UIColor *)lineColor {
_lineColor = lineColor;
[self.tableView reloadData];
}
#pragma mark - 私有方法
//16进制颜色字符串转为UIColor
- (NSMutableArray *)hexStringToColor:(NSString *)stringToConvert
{
NSString *cString = [[stringToConvert stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
if ([cString hasPrefix:@"0X"]) cString = [cString substringFromIndex:2];
if ([cString hasPrefix:@"#"]) cString = [cString substringFromIndex:1];
NSRange range;
range.location = 0;
range.length = 2;
NSString *rString = [cString substringWithRange:range];
range.location = 2;
NSString *gString = [cString substringWithRange:range];
range.location = 4;
NSString *bString = [cString substringWithRange:range];
unsigned int r, g, b;
[[NSScanner scannerWithString:rString] scanHexInt:&r];
[[NSScanner scannerWithString:gString] scanHexInt:&g];
[[NSScanner scannerWithString:bString] scanHexInt:&b];
return [NSMutableArray arrayWithObjects:@(r),@(g),@(b), nil];
}
#pragma mark - 覆盖drawRect方法,自定义绘画,画一个三角形
- (void)drawRect:(CGRect)rect{
//获取上下文
CGContextRef context = UIGraphicsGetCurrentContext();
//画三角形
CGPoint sPoint[3]; //定义三个坐标点
sPoint[0] = CGPointMake(_arrowOffset, 0); //第一个坐标
sPoint[1] = CGPointMake(_arrowOffset-8, 8); //第二个坐标
sPoint[2] = CGPointMake(_arrowOffset+8, 8); //第三个坐标
CGContextAddLines(context, sPoint, 3); //开始连线
//填充颜色
float r = [self.RGBStrValueArr[0] floatValue] / 255.0;
float g = [self.RGBStrValueArr[1] floatValue] / 255.0;
float b = [self.RGBStrValueArr[2] floatValue] / 255.0;
UIColor *stuffColor = [UIColor colorWithRed:r green:g blue:b alpha:1.0]; //颜色 定义跟tableView的背景也一致
//填充的颜色
CGContextSetFillColorWithColor(context, stuffColor.CGColor);
//线的颜色
CGContextSetRGBStrokeColor(context, r, g, b, 1.0);
//封装起来
CGContextClosePath(context);
//根据坐标开始连线 绘制路径
CGContextDrawPath(context, kCGPathFillStroke);
}
@end
接下来是单元格cell
YHDropMenuCell.h文件
#import#import "YHDropMenuModel.h"
@interface YHDropMenuCell : UITableViewCell
+ (instancetype)dropMenuCellWithTableView:(UITableView *)tableView;
// 数据模型
@property (nonatomic, strong) YHDropMenuModel *model;
// 图片
@property (nonatomic, strong) UIImageView *imageIV;
// 标题
@property (nonatomic, strong) UILabel *titleL;
// 线条
@property (nonatomic, strong) UIImageView *line1;
@end
YHDropMenuCell.m文件 实现部分
#import "YHDropMenuCell.h"
#define kDropMenuCellID @"DropMenuCellID"
@interface YHDropMenuCell()
@property (nonatomic,assign) CGFloat screenCenter; //蒙版的中心点
@end
@implementation YHDropMenuCell
+ (instancetype) dropMenuCellWithTableView:(UITableView *)tableView{
YHDropMenuCell *cell = [tableView dequeueReusableCellWithIdentifier:kDropMenuCellID];
if (!cell) {
cell = [[YHDropMenuCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kDropMenuCellID];
}
return cell;
}
-(instancetype) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
self.imageIV = [[UIImageView alloc] init];
[self.contentView addSubview:self.imageIV];
self.titleL = [[UILabel alloc] init];
self.titleL.font = [UIFont systemFontOfSize:15.f];
[self.contentView addSubview:self.titleL];
self.line1 = [[UIImageView alloc] init];
[self.contentView addSubview:self.line1];
self.selectionStyle = UITableViewCellSelectionStyleNone;
}
return self;
}
//UI布局
-(void)layoutSubviews{
[super layoutSubviews];
self.screenCenter = self.contentView.frame.size.height * 0.5;
self.imageIV.frame = CGRectMake(10, self.screenCenter - 8, 16, 16);
if (self.model.image == nil) {
self.titleL.frame = CGRectMake(10, self.screenCenter - 10, self.frame.size.width, 20);
}else{
self.titleL.frame = CGRectMake(CGRectGetMaxX(self.imageIV.frame) + 10, self.screenCenter - 10, self.contentView.frame.size.width - 50, 20);
}
self.line1.frame = CGRectMake(0, self.contentView.frame.size.height - 0.5, self.contentView.frame.size.width, 0.5);
}
//赋值
-(void)setModel:(YHDropMenuModel *)model{
_model = model;
self.imageIV.image = [UIImage imageNamed:model.image];
self.titleL.text = model.title;
}
@end
最后model 文件
YHDropMenuModel.h
#import <Foundation/Foundation.h>
@interface YHDropMenuModel : NSObject
//图片
@property (nonatomic, copy) NSString *image;
// 文字
@property (nonatomic, copy) NSString *title;
- (instancetype)initWithDictonary:(NSDictionary *)dict;
+ (instancetype)dropMenuWithDictonary:(NSDictionary *)dict;
@end
YHDropMenuModel.m 实现
#import "YHDropMenuModel.h"
@implementation YHDropMenuModel
- (instancetype)initWithDictonary:(NSDictionary *)dict {
if (self = [super init]) {
[self setValuesForKeysWithDictionary:dict];
}
return self;
}
+ (instancetype)dropMenuWithDictonary:(NSDictionary *)dict {
return [[self alloc] initWithDictonary:dict];
}
@end
到这里,下降菜单栏的功能就全部实现了