MenuView.m 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. //
  2. // MenuView.m
  3. // YHMenu
  4. //
  5. // Created by Boris on 2018/5/10.
  6. // Copyright © 2018年 Boris. All rights reserved.
  7. //
  8. #import "MenuView.h"
  9. #import "HEMenuTableCell.h"
  10. #define kHEMenuTableCell @"HEMenuTableCell"
  11. #define YHSafeAreaTopHeight (YHkHeight == 812.f ? 88.f : 64.f)
  12. #define YHkWidth [UIScreen mainScreen].bounds.size.width
  13. #define YHkHeight [UIScreen mainScreen].bounds.size.height
  14. #define kTriangleHeight 10.f //底的1/2倍
  15. #define kTriangleToHeight 10.f // 三角形的高,
  16. #define kItemHeight 40.f // item 高
  17. @implementation MenuView
  18. #pragma mark -------控件初始化------------
  19. - (NSMutableArray *)titleSource
  20. {
  21. if (!_titleSource) {
  22. self.titleSource = [NSMutableArray array];
  23. }
  24. return _titleSource;
  25. }
  26. - (NSMutableArray *)imgSource
  27. {
  28. if (!_imgSource) {
  29. self.imgSource = [NSMutableArray array];
  30. }
  31. return _imgSource;
  32. }
  33. - (UITableView *)tableView
  34. {
  35. if (!_tableView) {
  36. self.tableView = ({
  37. UITableView *tableView = [UITableView new];
  38. tableView.delegate = self;
  39. tableView.dataSource = self;
  40. [tableView registerClass:[HEMenuTableCell class] forCellReuseIdentifier:kHEMenuTableCell];
  41. tableView.tableFooterView = [UIView new];
  42. tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  43. tableView.showsVerticalScrollIndicator = NO;
  44. tableView.showsHorizontalScrollIndicator = NO;
  45. tableView;
  46. });
  47. }
  48. return _tableView;
  49. }
  50. #pragma mark ----------生命周期---------
  51. - (instancetype)initWithFrame:(CGRect)frame menuWidth:(CGFloat)width height:(CGFloat)height point:(CGPoint)point items:(NSArray *)items imgSource:(NSArray *)imgSource action:(void (^)(NSInteger row))action
  52. {
  53. self = [super initWithFrame:(frame)];
  54. if (self) {
  55. self.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
  56. self.point = CGPointMake(point.x, point.y);
  57. self.layerWidth = width;
  58. [self.titleSource removeAllObjects];
  59. [self.titleSource addObjectsFromArray:items];
  60. [self.imgSource removeAllObjects];
  61. [self.imgSource addObjectsFromArray:imgSource];
  62. if (_imgSource.count != _titleSource.count) {
  63. [_imgSource removeAllObjects];
  64. }
  65. if (imgSource.count == 0) {
  66. self.layerWidth = 100;
  67. }
  68. __weak typeof(self)weakSelf = self;
  69. if (action) {
  70. weakSelf.indexBlock = ^(NSInteger row) {
  71. action(row);
  72. };
  73. }
  74. self.layerHeight = _titleSource.count > 4 ? height : items.count*kItemHeight;
  75. [self addSubview:self.tableView];
  76. CGFloat y1 = self.point.y + kTriangleHeight;
  77. CGFloat x2 = self.point.x - self.layerWidth + kTriangleHeight +kTriangleHeight;
  78. _tableView.frame = CGRectMake(x2, y1, self.layerWidth, self.layerHeight);
  79. self.backgroundColor = [UIColor clearColor];
  80. }
  81. return self;
  82. }
  83. /***
  84. * x y
  85. * /\
  86. * x2 x1 x4 x3
  87. * |------- ----| y1
  88. * | |
  89. * | |
  90. * | |
  91. * | |
  92. * | |
  93. * |-------------| y2
  94. *
  95. *
  96. *
  97. ****************/
  98. #pragma mark 绘制三角形
  99. - (void)drawRect:(CGRect)rect
  100. {
  101. CGFloat y1 = self.point.y + kTriangleHeight;
  102. CGFloat y2 = y1 + self.layerHeight;
  103. CGFloat x1 = self.point.x - kTriangleHeight;
  104. CGFloat x2 = self.point.x - self.layerWidth + kTriangleHeight +kTriangleToHeight;
  105. CGFloat x3 = self.point.x + kTriangleHeight +kTriangleToHeight;
  106. CGFloat x4 = self.point.x + kTriangleHeight;
  107. // 设置背景色
  108. [[UIColor yellowColor] set];
  109. //拿到当前视图准备好的画板
  110. CGContextRef context = UIGraphicsGetCurrentContext();
  111. //利用path进行绘制三角形
  112. CGContextBeginPath(context);//标记
  113. // 起始点1
  114. CGContextMoveToPoint(context, self.point.x, self.point.y);//设置起点
  115. // 起始点2
  116. CGContextAddLineToPoint(context, x1, y1);
  117. // table左上角
  118. CGContextAddLineToPoint(context, x2, y1);
  119. // table左下角
  120. CGContextAddLineToPoint(context, x2, y2);
  121. // table 右下角
  122. CGContextAddLineToPoint(context, x3, y2);
  123. // table 右上角
  124. CGContextAddLineToPoint(context, x3, y1);
  125. // 起始点2
  126. CGContextAddLineToPoint(context,x4, y1);
  127. CGContextClosePath(context);//路径结束标志,不写默认封闭
  128. [[UIColor whiteColor] setFill]; //设置填充色
  129. [[UIColor whiteColor] setStroke]; //设置边框颜色
  130. CGContextDrawPath(context, kCGPathFillStroke);//绘制路径path
  131. }
  132. #pragma mark -----------UI事件------------
  133. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  134. {
  135. return self.titleSource.count;
  136. }
  137. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  138. {
  139. return kItemHeight;
  140. }
  141. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  142. {
  143. HEMenuTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
  144. if (!cell) {
  145. cell = [[HEMenuTableCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:kHEMenuTableCell width:self.layerWidth height:kItemHeight];
  146. }
  147. if (indexPath.row == 0) {
  148. cell.lineView.hidden = YES;
  149. }
  150. [cell setContentByTitArray:self.titleSource ImgArray:self.imgSource row:indexPath.row];
  151. cell.conLabel.text = self.titleSource[indexPath.row];
  152. return cell;
  153. }
  154. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  155. {
  156. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  157. if (self.indexBlock) {
  158. self.indexBlock(indexPath.row);
  159. }
  160. }
  161. - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
  162. {
  163. if (self.touchBlock) {
  164. self.touchBlock();
  165. }
  166. }
  167. /*
  168. // Only override drawRect: if you perform custom drawing.
  169. // An empty implementation adversely affects performance during animation.
  170. - (void)drawRect:(CGRect)rect {
  171. // Drawing code
  172. }
  173. */
  174. @end