// // QXMenuPopView.m // QXLive // // Created by 启星 on 2025/5/28. // #import "QXMenuPopView.h" @interface QXMenuPopView() @property (nonatomic,strong)UIView *arrowView; @property (nonatomic,strong)UIView *bgView; @property (nonatomic,assign)CGPoint point; @property (nonatomic,assign)CGFloat width; @property (nonatomic,assign)CGFloat height; @property (nonatomic,strong)UITableView *tableView; @end @implementation QXMenuPopView -(instancetype)initWithPoint:(CGPoint)point{ if (self = [super initWithFrame:[UIScreen mainScreen].bounds]) { self.frame = [UIScreen mainScreen].bounds; self.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.3]; _point = point; _width = 88; _height = 103; [self initSubviews]; } return self; } -(instancetype)initWithPoint:(CGPoint)point width:(CGFloat)width height:(CGFloat)height{ if (self = [super initWithFrame:[UIScreen mainScreen].bounds]) { self.frame = [UIScreen mainScreen].bounds; self.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.3]; _point = point; _width = width; _height = height; [self initSubviews]; } return self; } -(void)initSubviews{ UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hide)]; tap.delegate = self; [self addGestureRecognizer:tap]; self.bgView = [[UIView alloc] initWithFrame:CGRectMake(_point.x -_width/2, _point.y, _width, _height)]; self.bgView.clipsToBounds = YES; [self addSubview:self.bgView]; [self.bgView addSubview:self.tableView]; } -(void)setDataArray:(NSArray *)dataArray{ _dataArray = dataArray; [self.tableView reloadData]; } -(void)setType:(QXMenuPopViewType)type{ _type = type; switch (type) { case QXMenuPopViewTypeArrowTop: self.bgView.frame = CGRectMake(_point.x -_width/2, _point.y, _width, _height); self.arrowView.frame = CGRectMake((_width-12)/2, 3, 12, 12); self.tableView.frame = CGRectMake(0, self.arrowView.top+5, self.bgView.width, self.bgView.height-13); self.arrowView = [[UIView alloc] initWithFrame:CGRectMake((_width-12)/2, 3, 12, 12)]; self.arrowView.backgroundColor = [UIColor whiteColor]; self.arrowView.transform = CGAffineTransformMakeRotation(45 * M_PI/180.0); [self.bgView insertSubview:self.arrowView belowSubview:self.tableView]; break; case QXMenuPopViewTypeArrowBottom: self.bgView.frame = CGRectMake(_point.x -_width/2, _point.y, _width, _height); self.tableView.frame = CGRectMake(0, 0, self.bgView.width, self.bgView.height-16); self.arrowView = [[UIView alloc] initWithFrame:CGRectMake((_width-12)/2, self.tableView.bottom-8, 12, 12)]; self.arrowView.backgroundColor = [UIColor whiteColor]; self.arrowView.transform = CGAffineTransformMakeRotation(45 * M_PI/180.0); [self.bgView insertSubview:self.arrowView belowSubview:self.tableView]; break; default: break; } } -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{ return touch.view == self; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return self.dataArray.count; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"QXMenuCell"]; if (!cell) { cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"QXMenuCell"]; } cell.textLabel.text = self.dataArray[indexPath.row]; cell.textLabel.font = [UIFont systemFontOfSize:12]; cell.textLabel.textColor = RGB16(0x999999); return cell; } -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ [tableView deselectRowAtIndexPath:indexPath animated:YES]; [self hide]; if (self.delegate && [self.delegate respondsToSelector:@selector(didSelectedIndex:menuTitle:)]) { [self.delegate didSelectedIndex:indexPath.row menuTitle:self.dataArray[indexPath.row]]; } } -(UITableView *)tableView{ if (!_tableView) { self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.arrowView.top+5, self.bgView.width, self.bgView.height-13) style:UITableViewStylePlain]; self.tableView.backgroundColor = [UIColor whiteColor]; self.tableView.delegate = self; self.tableView.dataSource = self; self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; self.tableView.rowHeight = 30; self.tableView.bounces = NO; [self.tableView addRoundedCornersWithRadius:6]; } return _tableView; } -(void)showInView:(UIView *)view{ if (self.type == QXMenuPopViewTypeArrowTop) { self.bgView.height = 0; [view addSubview:self]; [UIView animateWithDuration:0.1 animations:^{ self.bgView.height = self.height; }]; }else{ self.bgView.height = 0; self.bgView.y = self.point.y+self.height; [view addSubview:self]; [UIView animateWithDuration:0.1 animations:^{ self.bgView.height = self.height; self.bgView.y = self.point.y; }]; } } -(void)hide{ if (self.type == QXMenuPopViewTypeArrowTop) { [UIView animateWithDuration:0.1 animations:^{ self.bgView.height = 0; } completion:^(BOOL finished) { [self removeFromSuperview]; }]; }else{ [UIView animateWithDuration:0.1 animations:^{ self.bgView.height = 0; self.bgView.y = self.point.y+self.height; } completion:^(BOOL finished) { [self removeFromSuperview]; }]; } } @end