Menu菜单-水平

作者: HeavenWong | 来源:发表于2016-05-03 11:21 被阅读47次
    • 已经封装好

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

    • .h文件

    #import <UIKit/UIKit.h>
    
    
    //需自定义按钮请修改此类
    @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 <NSObject>
    
    - (void)selectColum:(NSInteger)colum Row:(NSInteger)row Model:(chdModel*)model;
    
    @end
    
    @interface WMMenu : UIView<UITableViewDataSource,UITableViewDelegate>
    - (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 id<chdMenuDelegate>delegate;
    
    @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; i<arr.count; i++) {
                WMMenuButton *button = [[WMMenuButton alloc] initWithFrame:CGRectMake(i*(CGRectGetWidth(frame)/arr.count), 0, CGRectGetWidth(frame)/arr.count, CGRectGetHeight(frame))];
                button.tag = 100 + i;
                [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
                NSArray *temp = self.showArr[i];
                chdModel *model = [temp firstObject];
                [button setTitle:model.text forState:UIControlStateNormal];
                button.titleLabel.textAlignment = NSTextAlignmentCenter;
                button.backgroundColor = [UIColor whiteColor];
                button.layer.borderColor = [UIColor grayColor].CGColor;
                button.layer.borderWidth = 0.4;
                // 设置箭头图片
                [button setImage:[UIImage imageNamed:@"up1"] forState:UIControlStateNormal];
                button.imageView.transform = CGAffineTransformMakeRotation(M_PI);
                [button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
                [self addSubview:button];
                [view addSubview:self];
                
                //
                currentSelect = 0;
                [self selectClum:i Row:0];
                
            }
            
            ChdTable = [[UITableView alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(self.frame), CHD_SCREEN_WIDTH, CHD_SCREEN_HEIGHT - CGRectGetMaxY(self.frame))];
            orginalFrame = CGRectMake(0, CGRectGetMaxY(frame), CHD_SCREEN_WIDTH, 0);
            ChdTable.delegate = self;
            ChdTable.dataSource = self;
            ChdTable.hidden = YES;
            //ChdTable.backgroundColor = [UIColor redColor];
            
            bgView = [[UIView alloc] initWithFrame:orginalFrame];
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
            [tap addTarget:self action:@selector(bgViewClick:)];
            [bgView addGestureRecognizer:tap];
            bgView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.3];
            [view addSubview:bgView];
            
            if ([ ChdTable respondsToSelector:@selector(setSeparatorInset:)]) {
                
                [ChdTable   setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
                
            }
            if ([ChdTable respondsToSelector:@selector(setLayoutMargins:)]) {
                
                [ChdTable setLayoutMargins:UIEdgeInsetsMake(0, 0, 0, 0)];
                
            }
            
            [view addSubview:ChdTable];
            self.backgroundColor = [UIColor orangeColor];
        }
        
    }
    - (void)bgViewClick:(UITapGestureRecognizer*)tap
    {
        [self hideCurrent];
        bgView.frame = orginalFrame;
    }
    - (void)selectClum:(NSInteger)colum Row:(NSInteger)row
    {
        //默认选中第一个
        NSArray *temp = self.AllDataArr[colum];
        for (int i=0; i<temp.count; i++) {
            chdModel *model = temp[i];
            if (i == row) {
                model.isSelect = YES;
            }else{
                model.isSelect = NO;
            }
        }
        //[ChdTable reloadData];
        
        NSArray *arr = self.subviews;
        WMMenuButton *btn = (WMMenuButton*)arr[colum];
        chdModel *model = self.showArr[colum][row];
        [btn setTitle:model.text forState:UIControlStateNormal];
    }
    
    - (void)buttonClick:(WMMenuButton*)button
    {
        if (button.tag - 100 == currentSelect) {
            if (isShow) {
                [self hideCurrent];
            }else{
                [self showIndex:button.tag - 100];
            }
            isShow = !isShow;
        }else{
            [self showIndex:button.tag - 100];
            isShow = YES;
        }
    }
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return [_AllDataArr[currentSelect] count];
    }
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *chdResuseID = @"CHD_RESUSE";
        
        chdMenuCell *cell = [tableView dequeueReusableCellWithIdentifier:chdResuseID];
        
        if (cell == nil) {
            cell = [[chdMenuCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:chdResuseID];
        }
        
        chdModel * model = [_AllDataArr[currentSelect] objectAtIndex:indexPath.row];
        cell.textL.text = model.text;
        if (model.isSub) {
            cell.bgView.wm_x = 25;
        }else{
            cell.bgView.wm_x = 0;
        }
        if (model.isSelect) {
            cell.textL.textColor = [UIColor blueColor];
        }else{
            cell.textL.textColor = [UIColor blackColor];
        }
        return cell;
    }
    - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
    {
        if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
            
            [cell setSeparatorInset:UIEdgeInsetsZero];
            
        }
        
        if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
            
            [cell setLayoutMargins:UIEdgeInsetsZero];
            
        }
    }
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        return cellHeight;
    }
    - (void)showIndex:(NSInteger)index
    {
        
        if (currentSelect != index) {
            WMMenuButton *btn = self.subviews[currentSelect];
            [UIView animateWithDuration:0.2 animations:^{
                btn.imageView.transform = CGAffineTransformMakeRotation(M_PI);
            }];
        }
        
        currentSelect = index;
        [self realShow];
        
    }
    - (void)realShow
    {
        
        
        ChdTable.hidden = YES;
        ChdTable.wm_height = cellHeight * [_AllDataArr[currentSelect] count];
        [ChdTable reloadData];
        
        bgView.wm_height = CHD_SCREEN_HEIGHT - CGRectGetMaxY(self.frame);
        
        ChdTable.frame = orginalFrame;
        ChdTable.hidden = NO;
        
        WMMenuButton *btn = self.subviews[currentSelect];
        
        [UIView animateWithDuration:0.2 animations:^{
            if (cellHeight * [_AllDataArr[currentSelect] count]>CHD_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<row; j++) { //
                chdModel *model = [[chdModel alloc] init];
                model.text = [NSString stringWithFormat:@"第%@列---第%@行",@(i),@(j)];
                if (( i == 2 && 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<row; j++) {
                chdModel *model = [[chdModel alloc] init];
                model.text = [NSString stringWithFormat:@"%@-%@",@(i),@(j)];
                [temp addObject:model];
            }
            [ShowArr addObject:temp];
        }
        
        [[WMMenu alloc] initWithFrame:CGRectMake(0, 40,CHD_SCREEN_WIDTH, 40) showOnView:self.view AllDataArr:arr showArr:ShowArr];
        
        
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    @end
    
    效果图

    相关文章

      网友评论

      • 凡尘一笑:哥们,你这个封装,它的点击事件的处理,没有回调出来外面操作吗?

      本文标题:Menu菜单-水平

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