Menu菜单-水平

  • 已经封装好

  • 导入这个文件就可以使用

  • .h文件

#import 


//需自定义按钮请修改此类
@interface WMMenuButton : UIButton

@end
//需自定义数据模型请更改此类
@interface chdModel : NSObject
@property (nonatomic,copy) NSString *text;
@property BOOL isSub;
@property BOOL isSelect;
@end
//需自定义下拉cell请更改此类
@interface chdMenuCell : UITableViewCell
@property (nonatomic,retain) UIView *bgView;
@property (nonatomic,retain) UIView *point;
@property (nonatomic,retain) UILabel *textL;
@end

@protocol chdMenuDelegate 

- (void)selectColum:(NSInteger)colum Row:(NSInteger)row Model:(chdModel*)model;

@end

@interface WMMenu : UIView
- (void)initWithFrame:(CGRect)frame showOnView:(UIView*)view AllDataArr:(NSMutableArray*)arr showArr:(NSMutableArray *)showArr;
//使某列某行被选中,默认均选中第0行。 可调用此方法更改默认。
- (void)selectClum:(NSInteger)colum Row:(NSInteger)row;
@property (nonatomic,retain) NSMutableArray *AllDataArr;
@property (nonatomic,retain) NSMutableArray *showArr;
@property (nonatomic,weak) __weak iddelegate;

@end

  • .m文件
#import "WMMenu.h"
#import "UIView+WMExtension.h"
#define CHD_SCREEN_WIDTH ([[UIScreen mainScreen] bounds].size.width)
#define CHD_SCREEN_HEIGHT ([[UIScreen mainScreen] bounds].size.height)

static const CGFloat cellHeight = 40.0f;

@implementation WMMenuButton

- (CGRect)imageRectForContentRect:(CGRect)contentRect
{
    return CGRectMake(CGRectGetWidth(contentRect) - 20,(CGRectGetHeight(contentRect) - 7)/2, 12, 7);
}
- (CGRect)titleRectForContentRect:(CGRect)contentRect
{
    return CGRectMake(0, 0, CGRectGetWidth(contentRect) - 15, CGRectGetHeight(contentRect));
}
@end

@implementation chdModel

@end


@implementation chdMenuCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if ([super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CHD_SCREEN_WIDTH, cellHeight)];
        [self.contentView addSubview:_bgView];
        
        self.point = [[UIView alloc] initWithFrame:CGRectMake(10, (cellHeight - 3)/2.0, 3, 3)];
        self.point.layer.masksToBounds = YES;
        self.point.layer.cornerRadius = 1.5;
        [self.bgView addSubview:_point];
        
        self.textL = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(_point.frame)+5, 0,300, cellHeight)];
        self.textL.font = [UIFont systemFontOfSize:12.0f];
        [self.bgView addSubview:_textL];
        
    }
    return self;
}

@end

@implementation WMMenu
{
    UITableView *ChdTable;
    NSInteger currentSelect;
    CGRect orginalFrame;
    BOOL isShow;
    UIView *bgView;
}

- (void)initWithFrame:(CGRect)frame showOnView:(UIView*)view AllDataArr:(NSMutableArray*)arr showArr:(NSMutableArray *)showArr
{
    if ([super initWithFrame:frame]) {
        self.AllDataArr = arr;
        self.showArr = showArr;
        if (!showArr) {
            self.showArr = arr;
        }
        
        for (int i=0; iCHD_SCREEN_HEIGHT-CGRectGetMaxY(self.frame)) {
            ChdTable.wm_height = CHD_SCREEN_HEIGHT - CGRectGetMaxY(self.frame);
        }else{
            ChdTable.wm_height = cellHeight * [_AllDataArr[currentSelect] count];
        }
        btn.imageView.transform = CGAffineTransformMakeRotation(0);
    }];
}
- (void)hideCurrent
{
    WMMenuButton *btn = self.subviews[currentSelect];
    [UIView animateWithDuration:0.2 animations:^{
        ChdTable.frame = orginalFrame;
        btn.imageView.transform = CGAffineTransformMakeRotation(M_PI);
    }];
    bgView.frame = orginalFrame;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    //*************  本段代码用于选择相同选项时不在回调,不需要可注掉  ****************
    chdModel *modelLast = self.AllDataArr[currentSelect][indexPath.row];
    if (modelLast.isSelect) {
        [self hideCurrent];
        isShow = NO;
        
        return;
    }
    //*************  本段代码用于选择相同选项时不在回调,不需要可注掉  ****************
    
    
    
    [self selectClum:currentSelect Row:indexPath.row];
    [self hideCurrent];
    isShow = NO;
    chdModel *model = self.AllDataArr[currentSelect][indexPath.row];
    if ([self.delegate respondsToSelector:@selector(selectClum:Row:)]) {
        [self.delegate selectColum:currentSelect Row:indexPath.row Model:model];
    }
    NSLog(@"%@",model.text);
    
}

@end
  • 使用实例
#import "ViewController.h"
#import "WMMenu.h"
#define CHD_SCREEN_WIDTH ([[UIScreen mainScreen] bounds].size.width)

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //列表展示的模型
    NSMutableArray *arr = [NSMutableArray array];
    for (int i =0; i<3; i++) {// 3 列
        NSMutableArray *temp = [NSMutableArray array];//
        NSInteger row = (i+1)*3;// 3 6 9 行
        for (int j =0; j= 2 && j < 4) || ( i == 2 && j == 7)) {
                model.isSub = YES;
            }
            
            [temp addObject:model];// ==组的row
        }
        [arr addObject:temp]; // == 组
    }
    
    //上边按钮展示的模型,此模型对text赋值即可.
    NSMutableArray *ShowArr = [NSMutableArray array];
    for (int i =0; i<3; i++) { // 3列
        NSMutableArray *temp = [NSMutableArray array];
        NSInteger row = (i+1)*3;
        for (int j =0; j
Menu菜单-水平_第1张图片
效果图
  • [原文] http://www.jianshu.com/users/5fe7513c7a57/latest_articles

你可能感兴趣的:(Menu菜单-水平)