123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232 |
- //
- // QQPopMenuView.m
- // ECLite
- //
- // Created by ec on 16/6/16.
- // Copyright © 2016年 eclite. All rights reserved.
- //
- #import "QQPopMenuView.h"
- #ifndef SCREEN_WIDTH
- #define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
- #endif
- static CGFloat const kCellHeight = 44;
- @interface PopMenuTableViewCell : UITableViewCell
- @property (nonatomic, strong) UIImageView *leftImageView;
- @property (nonatomic, strong) UILabel *titleLabel;
- @end
- @interface QQPopMenuView ()<UITableViewDelegate,UITableViewDataSource,UIGestureRecognizerDelegate>
- @property (nonatomic, strong) UITableView *tableView;
- @property (nonatomic, strong) NSArray *tableData;
- @property (nonatomic, assign) CGPoint trianglePoint;
- @property (nonatomic, copy) void(^action)(NSInteger index);
- @end
- @implementation QQPopMenuView
- - (instancetype)initWithItems:(NSArray <NSDictionary *>*)array
- width:(CGFloat)width
- triangleLocation:(CGPoint)point
- action:(void(^)(NSInteger index))action
- {
- if (array.count == 0) return nil;
- if (self = [super init]) {
- self.frame = [UIScreen mainScreen].bounds;
- self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.2];
- self.alpha = 0;
- _tableData = [array copy];
- _trianglePoint = point;
- self.action = action;
-
-
- // 添加手势
- UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)];
- tap.delegate = self;
- [self addGestureRecognizer:tap];
-
-
- // 创建tableView
- _tableView = [[UITableView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - width - 5, point.y + 10, width, kCellHeight * array.count) style:UITableViewStylePlain];
- _tableView.delegate = self;
- _tableView.dataSource = self;
- _tableView.backgroundColor = [UIColor whiteColor];
- _tableView.layer.masksToBounds = YES;
- _tableView.layer.cornerRadius = 5;
- _tableView.scrollEnabled = NO;
- _tableView.rowHeight = kCellHeight;
- [_tableView registerClass:[PopMenuTableViewCell class] forCellReuseIdentifier:@"PopMenuTableViewCell"];
- if (@available(iOS 15.0, *)) {
- _tableView.sectionHeaderTopPadding = 0;
- }
- [self addSubview:_tableView];
-
- }
- return self;
- }
- + (void)showWithItems:(NSArray <NSDictionary *>*)array
- width:(CGFloat)width
- triangleLocation:(CGPoint)point
- action:(void(^)(NSInteger index))action
- {
- QQPopMenuView *view = [[QQPopMenuView alloc] initWithItems:array width:width triangleLocation:point action:action];
-
- [view show];
- }
- - (void)tap {
- [self hide];
- }
- #pragma mark - UIGestureRecognizerDelegate
- - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
- if ([touch.view isKindOfClass:NSClassFromString(@"UITableViewCellContentView")]) {
- return NO;
- }
- return YES;
- }
- #pragma mark - show or hide
- - (void)show {
- [[UIApplication sharedApplication].keyWindow addSubview:self];
- // 设置右上角为transform的起点(默认是中心点)
- _tableView.layer.position = CGPointMake(SCREEN_WIDTH - 5, _trianglePoint.y + 10);
- // 向右下transform
- _tableView.layer.anchorPoint = CGPointMake(1, 0);
- _tableView.transform = CGAffineTransformMakeScale(0.0001, 0.0001);
- [UIView animateWithDuration:0.2 animations:^{
- self.alpha = 1;
- _tableView.transform = CGAffineTransformMakeScale(1.0, 1.0);
- }];
- }
- - (void)hide {
- [UIView animateWithDuration:0.2 animations:^{
- self.alpha = 0;
- _tableView.transform = CGAffineTransformMakeScale(0.0001, 0.0001);
- } completion:^(BOOL finished) {
- [_tableView removeFromSuperview];
- [self removeFromSuperview];
- if (self.hideHandle) {
- self.hideHandle();
- }
- }];
- }
- #pragma mark - Draw triangle
- - (void)drawRect:(CGRect)rect {
- // 设置背景色
- [[UIColor whiteColor] set];
- //拿到当前视图准备好的画板
- CGContextRef context = UIGraphicsGetCurrentContext();
- //利用path进行绘制三角形
- CGContextBeginPath(context);
- CGPoint point = _trianglePoint;
- // 设置起点
- CGContextMoveToPoint(context, point.x, point.y);
- // 画线
- CGContextAddLineToPoint(context, point.x - 10, point.y + 10);
- CGContextAddLineToPoint(context, point.x + 10, point.y + 10);
- CGContextClosePath(context);
- // 设置填充色
- [[UIColor whiteColor] setFill];
- // 设置边框颜色
- [[UIColor whiteColor] setStroke];
- // 绘制路径
- CGContextDrawPath(context, kCGPathFillStroke);
- }
- #pragma mark - UITableViewDataSource
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
- return self.tableData.count;
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
- PopMenuTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PopMenuTableViewCell" forIndexPath:indexPath];
- NSDictionary *dic = _tableData[indexPath.row];
-
- if ([dic.allKeys containsObject:@"imageName"]) {
- [cell.leftImageView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.offset(12.0f);
- make.size.mas_offset(CGSizeMake(22.0f, 22.0f));
- make.centerY.equalTo(cell.contentView);
- }];
- [cell.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.equalTo(cell.leftImageView.mas_right).offset(12.0f);
- make.centerY.equalTo(cell.contentView);
- make.right.offset(-12.0f);
- }];
- }else{
- [cell.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.offset(12.0f);
- make.centerY.equalTo(cell.contentView);
- make.right.offset(-12.0f);
- }];
- }
-
- cell.leftImageView.image = [UIImage imageNamed:dic[@"imageName"]];
- cell.titleLabel.text = dic[@"title"];
- // [cell.titleLabel sizeToFit];
-
- cell.selectionStyle = UITableViewCellSelectionStyleNone;
- // cell.layoutMargins = UIEdgeInsetsZero;
- cell.separatorInset = UIEdgeInsetsMake(0, KScreenWidth, 0, 0);
- return cell;
- }
- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
- [self hide];
- if (_action) {
- _action(indexPath.row);
- }
- }
- @end
- @implementation PopMenuTableViewCell
- - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
- self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
- if (self) {
-
- _leftImageView = [[UIImageView alloc] init];
- [self.contentView addSubview:_leftImageView];
-
- _titleLabel = [[UILabel alloc] init];
- _titleLabel.font = [UIFont systemFontOfSize:12];
- _titleLabel.textColor = DecColorFromRGB(23, 26, 29);
- [self.contentView addSubview:_titleLabel];
- UIView *viewLine =[UIView new];
- [self.contentView addSubview:viewLine];
- [viewLine mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.right.mas_equalTo(0);
- make.bottom.mas_equalTo(0);
- make.height.mas_equalTo(1);
- }];
- viewLine.backgroundColor = HexColorFromRGB(0xEDEDF0);
-
- self.contentView.backgroundColor = [UIColor whiteColor];
- }
- return self;
- }
- - (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
- [super setHighlighted:highlighted animated:animated];
-
- if (highlighted) {
- self.backgroundColor = [UIColor whiteColor];
- }else {
- self.backgroundColor = [UIColor whiteColor];
- }
- }
- @end
|