美文网首页
封装下拉上拉菜单

封装下拉上拉菜单

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

效果图:


下拉菜单.jpg
上拉菜单.jpg

首先,新建一个继承于UIView的类DropDownMenuView。

DropDownMenuView.h

//
//  DropDownMenuView.h
//  DropDownMenu
//
//  Created by  刘雅兰 on 2017/11/21.
//  Copyright © 2017年  刘雅兰. All rights reserved.
//

#import <UIKit/UIKit.h>
//@protocol DropDownDelegate <NSObject>
//
///*
// 代理方法还需要知道是点击的哪一行
// */
//-(void)dropDownListParame:(NSString *)aStr;
//
//@end
typedef void (^CallingMethod)(NSString *aStr,NSInteger i);
typedef NS_ENUM(NSInteger,kind){
    kindOne,
    kindTwo,
};


@interface DropDownMenuView : UIView

@property (nonatomic, assign) CGFloat cellHeight;//cell行高
@property (nonatomic,strong) NSArray *array;

@property (nonatomic,strong) UIView *v;

@property (nonatomic,strong) UIColor *selectColor;
@property (nonatomic,strong) UIColor *normalColor;

@property (nonatomic,strong) CallingMethod value;

@property (nonatomic,assign) kind kind;
/*
不要再声明额外的构造方法,直接就使用initWithFrame就行了,相关属性暴露在h文件中就行
 */
//-(id)initWithListDataSource:(NSArray *)array
//                  cellHeight:(CGFloat)cellHeight
//                       view:(UIView *)v;
//@property (assign,nonatomic) id<DropDownDelegate>delegate;
-(void)showList;
-(void)closeList;

-(void)beginCallingMethod:(CallingMethod)val;

@end

DropDownMenuView.m

//
//  DropDownMenuView.m
//  DropDownMenu
//
//  Created by  刘雅兰 on 2017/11/21.
//  Copyright © 2017年  刘雅兰. All rights reserved.
//

#import "DropDownMenuView.h"
#import "DropTableViewCell.h"
#define SCREEN_WIDTH [[UIScreen mainScreen] bounds].size.width
#define SCREEN_HEIGHT [[UIScreen mainScreen] bounds].size.height
#define Cell @"Cell"

@interface DropDownMenuView ()<UITableViewDelegate,UITableViewDataSource>


@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIView *listView;
@property (nonatomic,strong) UIView *view;
@property(nonatomic,assign)NSInteger index;    //记录选中行
@property(nonatomic,strong)UIButton *button;

@end
@implementation DropDownMenuView
-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.array = _array;
        self.cellHeight = _cellHeight;
        self.button = (UIButton *)_v;
        

     _listView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];

    _listView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
        UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeList)];

        [_listView addGestureRecognizer:tapGesture];
        [self addSubview:_listView];

        


        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0) style:UITableViewStylePlain];

        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerNib:[UINib nibWithNibName:@"DropTableViewCell" bundle:nil] forCellReuseIdentifier:Cell];
        [self addSubview:_tableView];

        self.hidden = YES;
    }
    return self;
    
}

-(void)setKind:(kind)kind{
    _kind = kind;
    if (_kind == kindTwo) {

        _listView.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, -SCREEN_HEIGHT);

        _tableView.frame =CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, 0);

        
    }
}

-(UIColor *)selectColor{
    if (_selectColor == nil) {
        _selectColor = [UIColor colorWithRed:0x30/255.0 green:0x7a/255.0 blue:0xff/255.0 alpha:1.0];
    }
    return _selectColor;
    
}
-(UIColor *)normalColor{
    if (_normalColor == nil) {
        _normalColor = [UIColor blackColor];
    }
    return _normalColor;
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.array.count;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
    DropTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Cell forIndexPath:indexPath];
    cell.labelTitle.text = _array[indexPath.row];
    if (self.index == indexPath.row)
    {
        cell.img.hidden = NO;
        cell.labelTitle.textColor=self.selectColor;
    }else{
        cell.img.hidden = YES;
        cell.labelTitle.textColor=self.normalColor;
    }
    
    return cell;
    
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    self.index = indexPath.row;
    if ([self respondsToSelector:@selector(beginCallingMethod:)]) {
       [self beginCallingMethod:^(NSString *aStr, NSInteger i) {
            aStr=self.array[indexPath.row];
            i=self.index;
       }];
    }
    [_tableView reloadData];
    [self closeList];
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return self.cellHeight;
}

//显示
-(void)showList{
//    _listView.hidden = NO;
//    _tableView.hidden = NO;
//    [_tableView reloadData];
//    [UIView animateWithDuration:0.25f animations:^{
//        _listView.alpha = 1;
//        switch (_kind) {
//            case kindOne:
//                _tableView.frame = CGRectMake(0, 0, SCREEN_WIDTH, self.cellHeight * self.array.count);
//
//                break;
//               case kindTwo:
//                _tableView.frame = CGRectMake(0, SCREEN_HEIGHT-self.cellHeight * self.array.count, SCREEN_WIDTH, self.cellHeight *self.array.count);
//
//                break;
//            default:
//                break;
//        }
//     }];
//
    
    if (!self.hidden) {
        [self closeList];
        return;
    }
    self.hidden = NO;
    [UIView animateWithDuration:0.25f animations:^{
        _listView.alpha = 0.5;
        switch (_kind) {
            case kindOne:
                _tableView.frame = CGRectMake(0, 0, SCREEN_WIDTH, self.cellHeight * self.array.count);
                
                break;
            case kindTwo:
                _tableView.frame = CGRectMake(0, SCREEN_HEIGHT-self.cellHeight * self.array.count, SCREEN_WIDTH, self.cellHeight *self.array.count);
                
                break;
            default:
                break;
        }
    }];
    
    
    
    
    
    
    
}
//关闭
-(void)closeList{
    [UIView animateWithDuration:0.25f animations:^{

        _listView.alpha = 0;
        switch (_kind) {
            case kindOne:
                _tableView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 0);                      break;
              case kindTwo:
               _tableView.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, 0);
                break;
            default:
                break;
        }
        
    } completion:^(BOOL finished) {

        self.hidden = YES;

    }];
    
    
}


-(void)beginCallingMethod:(CallingMethod)val{
    _value(self.array[self.index],_index);

}

新建一个TableViewCell,此处cell样式为,左边一个label,右边一个imageView。

最后,在ViewController里面使用封装的上拉下拉菜单:
ViewController.m

//
//  DropDownMenuViewController.m
//  DropDownMenu
//
//  Created by  刘雅兰 on 2017/11/21.
//  Copyright © 2017年  刘雅兰. All rights reserved.
//

#import "DropDownMenuViewController.h"
#import "DropDownMenuView.h"

@interface DropDownMenuViewController ()
@property (weak, nonatomic) IBOutlet UIView *mainView;
@property (nonatomic, strong) NSArray *array;
@property (nonatomic, strong) DropDownMenuView *list;
@property (nonatomic,strong) DropDownMenuView *upList;
@property (weak, nonatomic) IBOutlet UIButton *dropDown;
@property (weak, nonatomic) IBOutlet UIButton *upDown;



@end

@implementation DropDownMenuViewController

- (void)viewDidLoad {
    [super viewDidLoad];
     _array = [NSArray arrayWithObjects:@"第一行",@"第二行",@"第三行",@"第四行",@"第五行",@"第六行",@"拜拜",nil];
     _list = [[DropDownMenuView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
     _list.array=_array;
     _list.cellHeight = 44;
     _list.v=self.dropDown;
     _list.value = ^(NSString *aStr, NSInteger i) {
           [_dropDown setTitle:[NSString stringWithFormat:@"%@",aStr] forState:UIControlStateNormal];
               NSLog(@"%ld",(long)i);

    };
    _list.kind = kindOne;
    
   
    _upList = [[DropDownMenuView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    _upList.array=_array;
    _upList.cellHeight = 44;
    _upList.v=self.upDown;
    _upList.value = ^(NSString *aStr, NSInteger i) {
        [_upDown setTitle:[NSString stringWithFormat:@"%@",aStr] forState:UIControlStateNormal];
    };
    _upList.kind = kindTwo;
    

    
    
    [self.mainView addSubview:_upList];
    [self.mainView addSubview:_list];
    
  
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)dropDowm:(id)sender {


        [_list showList];

}
- (IBAction)upDown:(id)sender {
    
    [_upList showList];
    
    
}



-(DropDownMenuView *)list
{
    if (!_list)
    {
        
    }
    return _list;
}

-(DropDownMenuView *)upList{
    if (!_upList) {
        
    }
    return _upList;
}





@end

ok。刚开始回调用的代理方法,后来改为了现在的Block。
demo链接:https://gitee.com/LanXiaoMei/XiaLaShangLaCaiDan/tree/master

相关文章

网友评论

      本文标题:封装下拉上拉菜单

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