首页 > 代码库 > 弹出菜单

弹出菜单

封装的一个方法

.h文件中

#import <UIKit/UIKit.h>

@protocol LrdOutputViewDelegate <NSObject>

 @required

- (void)didSelectedAtIndexPath:(NSIndexPath *)indexPath;

 @end

 typedef void(^dismissWithOperation)();

 typedef NS_ENUM(NSUInteger, LrdOutputViewDirection) {

    kLrdOutputViewDirectionLeft = 1,

    kLrdOutputViewDirectionRight

};

 @interface LrdOutputView : UIView

 @property (nonatomic, weak) id<LrdOutputViewDelegate> delegate;

@property (nonatomic, strong) dismissWithOperation dismissOperation;

 //初始化方法

//传入参数:模型数组,弹出原点,宽度,高度(每个cell的高度)

- (instancetype)initWithDataArray:(NSArray *)dataArray

                           origin:(CGPoint)origin

                            width:(CGFloat)width

                           height:(CGFloat)height

                        direction:(LrdOutputViewDirection)direction;

 

//弹出

- (void)pop;

//消失

- (void)dismiss;

 @end

 @interface LrdCellModel : NSObject

 @property (nonatomic, copy) NSString *title;

@property (nonatomic, copy) NSString *imageName;

 - (instancetype)initWithTitle:(NSString *)title imageName:(NSString *)imageName;

.m文件中

宏定义

#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width

#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height

#define CellLineEdgeInsets UIEdgeInsetsMake(0, 10, 0, 10)

#define LeftToView 10.f

#define TopToView 10.f

在@interface里面声明属性

最后在@implementation中实现方法

- (instancetype)initWithDataArray:(NSArray *)dataArray

                           origin:(CGPoint)origin

                            width:(CGFloat)width

                           height:(CGFloat)height

                        direction:(LrdOutputViewDirection)direction {

    if (self = [super initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)]) {

        //背景色为clearColor

        self.backgroundColor = [UIColor clearColor];

        self.origin = origin;

        self.height = height;

        self.width = width;

        self.direction = direction;

        self.dataArray = dataArray;

        if (height <= 0) {

            height = 44;

        }

        if (direction == kLrdOutputViewDirectionLeft) {

            self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(origin.x, origin.y, width, height * _dataArray.count) style:UITableViewStylePlain];

        }else {

            self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(origin.x, origin.y, -width, height * _dataArray.count) style:UITableViewStylePlain];

        }

        

        _tableView.separatorColor = [UIColor colorWithWhite:0.3 alpha:1];

        _tableView.backgroundColor = [UIColor colorWithWhite:0.2 alpha:1];

        _tableView.bounces = NO;

        _tableView.layer.cornerRadius = 2;

        _tableView.delegate = self;

        _tableView.dataSource = self;

        //注册cell

        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];

       [self addSubview:self.tableView];

        //cell线条

        if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {

            [self.tableView setSeparatorInset:CellLineEdgeInsets];

        }

        if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {

            [self.tableView setLayoutMargins:CellLineEdgeInsets];

        }

    }

    return self;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.dataArray.count;

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    return self.height;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

    cell.textLabel.textColor = [UIColor whiteColor];

    cell.textLabel.font = [UIFont systemFontOfSize:15];

    cell.backgroundColor = [UIColor clearColor];

    //取出模型

    LrdCellModel *model = [self.dataArray objectAtIndex:indexPath.row];

    cell.textLabel.text = model.title;

    cell.imageView.image = [UIImage imageNamed:model.imageName];

    cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];

    cell.selectedBackgroundView.backgroundColor = [UIColor blackColor];

 return cell;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    //通知代理处理点击事件

    if ([self.delegate respondsToSelector:@selector(didSelectedAtIndexPath:)]) {

        [self.delegate didSelectedAtIndexPath:indexPath];

    }

    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    [self dismiss];

}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {

    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {

        [cell setSeparatorInset:CellLineEdgeInsets];

    }

    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {

        [cell setLayoutMargins:CellLineEdgeInsets];

    }

}

//画出尖尖

- (void)drawRect:(CGRect)rect {

    //拿到当前视图准备好的画板

    CGContextRef context = UIGraphicsGetCurrentContext();

    //利用path进行绘制三角形

    CGContextBeginPath(context);//标记

     if (self.direction == kLrdOutputViewDirectionLeft) {

        CGFloat startX = self.origin.x + 20;

        CGFloat startY = self.origin.y;

        CGContextMoveToPoint(context, startX, startY);//设置起点

        CGContextAddLineToPoint(context, startX + 5, startY - 5);

        CGContextAddLineToPoint(context, startX + 10, startY);

    }else {

        CGFloat startX = self.origin.x - 20;

        CGFloat startY = self.origin.y;

        CGContextMoveToPoint(context, startX, startY);//设置起点

        CGContextAddLineToPoint(context, startX + 5, startY - 5);

        CGContextAddLineToPoint(context, startX + 10, startY);

    }

    CGContextClosePath(context);//路径结束标志,不写默认封闭

    [self.tableView.backgroundColor setFill]; //设置填充色

    [self.tableView.backgroundColor setStroke];

    CGContextDrawPath(context, kCGPathFillStroke);//绘制路径path;

}

 - (void)pop {

    UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];

    [keyWindow addSubview:self];

    //动画效果弹出

    self.alpha = 0;

    CGRect frame = self.tableView.frame;

    self.tableView.frame = CGRectMake(self.origin.x, self.origin.y, 0, 0);

    [UIView animateWithDuration:0.2 animations:^{

        self.alpha = 1;

        self.tableView.frame = frame;

    }];

}

- (void)dismiss {

    //动画效果淡出

    [UIView animateWithDuration:0.2 animations:^{

        self.alpha = 0;

        self.tableView.frame = CGRectMake(self.origin.x, self.origin.y, 0, 0);

    } completion:^(BOOL finished) {

        if (finished) {

            [self removeFromSuperview];

            if (self.dismissOperation) {

                self.dismissOperation();

            }

        }

    }];

}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

    UITouch *touch = [touches anyObject];

    if (![touch.view isEqual:self.tableView]) {

        [self dismiss];

    }

}

@end

#pragma mark - LrdCellModel

@implementation LrdCellModel

- (instancetype)initWithTitle:(NSString *)title imageName:(NSString *)imageName {

    LrdCellModel *model = [[LrdCellModel alloc] init];

    model.title = title;

    model.imageName = imageName;

    return model;

}

 

弹出菜单