QQPopMenuView.m 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. //
  2. // QQPopMenuView.m
  3. // ECLite
  4. //
  5. // Created by ec on 16/6/16.
  6. // Copyright © 2016年 eclite. All rights reserved.
  7. //
  8. #import "QQPopMenuView.h"
  9. #ifndef SCREEN_WIDTH
  10. #define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
  11. #endif
  12. static CGFloat const kCellHeight = 44;
  13. @interface PopMenuTableViewCell : UITableViewCell
  14. @property (nonatomic, strong) UIImageView *leftImageView;
  15. @property (nonatomic, strong) UILabel *titleLabel;
  16. @end
  17. @interface QQPopMenuView ()<UITableViewDelegate,UITableViewDataSource,UIGestureRecognizerDelegate>
  18. @property (nonatomic, strong) UITableView *tableView;
  19. @property (nonatomic, strong) NSArray *tableData;
  20. @property (nonatomic, assign) CGPoint trianglePoint;
  21. @property (nonatomic, copy) void(^action)(NSInteger index);
  22. @end
  23. @implementation QQPopMenuView
  24. - (instancetype)initWithItems:(NSArray <NSDictionary *>*)array
  25. width:(CGFloat)width
  26. triangleLocation:(CGPoint)point
  27. action:(void(^)(NSInteger index))action
  28. {
  29. if (array.count == 0) return nil;
  30. if (self = [super init]) {
  31. self.frame = [UIScreen mainScreen].bounds;
  32. self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.2];
  33. self.alpha = 0;
  34. _tableData = [array copy];
  35. _trianglePoint = point;
  36. self.action = action;
  37. // 添加手势
  38. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)];
  39. tap.delegate = self;
  40. [self addGestureRecognizer:tap];
  41. // 创建tableView
  42. _tableView = [[UITableView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - width - 5, point.y + 10, width, kCellHeight * array.count) style:UITableViewStylePlain];
  43. _tableView.delegate = self;
  44. _tableView.dataSource = self;
  45. _tableView.backgroundColor = [UIColor whiteColor];
  46. _tableView.layer.masksToBounds = YES;
  47. _tableView.layer.cornerRadius = 5;
  48. _tableView.scrollEnabled = NO;
  49. _tableView.rowHeight = kCellHeight;
  50. [_tableView registerClass:[PopMenuTableViewCell class] forCellReuseIdentifier:@"PopMenuTableViewCell"];
  51. if (@available(iOS 15.0, *)) {
  52. _tableView.sectionHeaderTopPadding = 0;
  53. }
  54. [self addSubview:_tableView];
  55. }
  56. return self;
  57. }
  58. + (void)showWithItems:(NSArray <NSDictionary *>*)array
  59. width:(CGFloat)width
  60. triangleLocation:(CGPoint)point
  61. action:(void(^)(NSInteger index))action
  62. {
  63. QQPopMenuView *view = [[QQPopMenuView alloc] initWithItems:array width:width triangleLocation:point action:action];
  64. [view show];
  65. }
  66. - (void)tap {
  67. [self hide];
  68. }
  69. #pragma mark - UIGestureRecognizerDelegate
  70. - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
  71. if ([touch.view isKindOfClass:NSClassFromString(@"UITableViewCellContentView")]) {
  72. return NO;
  73. }
  74. return YES;
  75. }
  76. #pragma mark - show or hide
  77. - (void)show {
  78. [[UIApplication sharedApplication].keyWindow addSubview:self];
  79. // 设置右上角为transform的起点(默认是中心点)
  80. _tableView.layer.position = CGPointMake(SCREEN_WIDTH - 5, _trianglePoint.y + 10);
  81. // 向右下transform
  82. _tableView.layer.anchorPoint = CGPointMake(1, 0);
  83. _tableView.transform = CGAffineTransformMakeScale(0.0001, 0.0001);
  84. [UIView animateWithDuration:0.2 animations:^{
  85. self.alpha = 1;
  86. _tableView.transform = CGAffineTransformMakeScale(1.0, 1.0);
  87. }];
  88. }
  89. - (void)hide {
  90. [UIView animateWithDuration:0.2 animations:^{
  91. self.alpha = 0;
  92. _tableView.transform = CGAffineTransformMakeScale(0.0001, 0.0001);
  93. } completion:^(BOOL finished) {
  94. [_tableView removeFromSuperview];
  95. [self removeFromSuperview];
  96. if (self.hideHandle) {
  97. self.hideHandle();
  98. }
  99. }];
  100. }
  101. #pragma mark - Draw triangle
  102. - (void)drawRect:(CGRect)rect {
  103. // 设置背景色
  104. [[UIColor whiteColor] set];
  105. //拿到当前视图准备好的画板
  106. CGContextRef context = UIGraphicsGetCurrentContext();
  107. //利用path进行绘制三角形
  108. CGContextBeginPath(context);
  109. CGPoint point = _trianglePoint;
  110. // 设置起点
  111. CGContextMoveToPoint(context, point.x, point.y);
  112. // 画线
  113. CGContextAddLineToPoint(context, point.x - 10, point.y + 10);
  114. CGContextAddLineToPoint(context, point.x + 10, point.y + 10);
  115. CGContextClosePath(context);
  116. // 设置填充色
  117. [[UIColor whiteColor] setFill];
  118. // 设置边框颜色
  119. [[UIColor whiteColor] setStroke];
  120. // 绘制路径
  121. CGContextDrawPath(context, kCGPathFillStroke);
  122. }
  123. #pragma mark - UITableViewDataSource
  124. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  125. return self.tableData.count;
  126. }
  127. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  128. PopMenuTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PopMenuTableViewCell" forIndexPath:indexPath];
  129. NSDictionary *dic = _tableData[indexPath.row];
  130. if ([dic.allKeys containsObject:@"imageName"]) {
  131. [cell.leftImageView mas_makeConstraints:^(MASConstraintMaker *make) {
  132. make.left.offset(12.0f);
  133. make.size.mas_offset(CGSizeMake(22.0f, 22.0f));
  134. make.centerY.equalTo(cell.contentView);
  135. }];
  136. [cell.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  137. make.left.equalTo(cell.leftImageView.mas_right).offset(12.0f);
  138. make.centerY.equalTo(cell.contentView);
  139. make.right.offset(-12.0f);
  140. }];
  141. }else{
  142. [cell.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  143. make.left.offset(12.0f);
  144. make.centerY.equalTo(cell.contentView);
  145. make.right.offset(-12.0f);
  146. }];
  147. }
  148. cell.leftImageView.image = [UIImage imageNamed:dic[@"imageName"]];
  149. cell.titleLabel.text = dic[@"title"];
  150. // [cell.titleLabel sizeToFit];
  151. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  152. // cell.layoutMargins = UIEdgeInsetsZero;
  153. cell.separatorInset = UIEdgeInsetsMake(0, KScreenWidth, 0, 0);
  154. return cell;
  155. }
  156. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  157. [self hide];
  158. if (_action) {
  159. _action(indexPath.row);
  160. }
  161. }
  162. @end
  163. @implementation PopMenuTableViewCell
  164. - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
  165. self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  166. if (self) {
  167. _leftImageView = [[UIImageView alloc] init];
  168. [self.contentView addSubview:_leftImageView];
  169. _titleLabel = [[UILabel alloc] init];
  170. _titleLabel.font = [UIFont systemFontOfSize:12];
  171. _titleLabel.textColor = DecColorFromRGB(23, 26, 29);
  172. [self.contentView addSubview:_titleLabel];
  173. UIView *viewLine =[UIView new];
  174. [self.contentView addSubview:viewLine];
  175. [viewLine mas_makeConstraints:^(MASConstraintMaker *make) {
  176. make.left.right.mas_equalTo(0);
  177. make.bottom.mas_equalTo(0);
  178. make.height.mas_equalTo(1);
  179. }];
  180. viewLine.backgroundColor = HexColorFromRGB(0xEDEDF0);
  181. self.contentView.backgroundColor = [UIColor whiteColor];
  182. }
  183. return self;
  184. }
  185. - (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
  186. [super setHighlighted:highlighted animated:animated];
  187. if (highlighted) {
  188. self.backgroundColor = [UIColor whiteColor];
  189. }else {
  190. self.backgroundColor = [UIColor whiteColor];
  191. }
  192. }
  193. @end