popMenuView.m 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. //
  2. // popMenuView.m
  3. // popViewDemo
  4. //
  5. // Created by GG on 2019/1/15.
  6. // Copyright © 2019年 Haishenghai intelligence network technology. All rights reserved.
  7. //
  8. #import "popMenuView.h"
  9. #define LeftView 10.0f
  10. #define TopToView 10.0f
  11. @interface popMenuView()<UITableViewDataSource,UITableViewDelegate>
  12. @property (nonatomic,copy) NSArray *selectData;
  13. @property (nonatomic,copy) void(^action)(NSInteger index);
  14. @property (nonatomic,copy) NSArray * imagesData;
  15. @end
  16. popMenuView * backgroundView;
  17. UITableView * tableView;
  18. @implementation popMenuView
  19. - (instancetype)initWithFrame:(CGRect)frame{
  20. if (self = [super initWithFrame:frame]) {
  21. }
  22. return self;
  23. }
  24. +(void)addPopTableViewSelectWithFrame:(CGRect)frame selectData:(NSArray *)select images:(NSArray *)leftImage action:(void (^)(NSInteger))action animation:(BOOL)animaton{
  25. if (backgroundView != nil) {
  26. [popMenuView hiden];
  27. }
  28. UIWindow *win = [[[UIApplication sharedApplication] windows] firstObject];
  29. backgroundView = [[popMenuView alloc] initWithFrame:win.bounds];
  30. backgroundView.action = action;
  31. backgroundView.imagesData = leftImage ;
  32. backgroundView.selectData = select;
  33. backgroundView.backgroundColor = [UIColor colorWithHue:0
  34. saturation:0
  35. brightness:0 alpha:0.1];
  36. [win addSubview:backgroundView];
  37. // TAB
  38. tableView = [[UITableView alloc] initWithFrame:CGRectMake( [UIScreen mainScreen].bounds.size.width - 80 , 70.0 - 20.0 * select.count , frame.size.width, 40 * select.count) style:0];
  39. tableView.dataSource = backgroundView;
  40. // tableView.transform = CGAffineTransformMakeScale(0.5, 0.5);
  41. tableView.delegate = backgroundView;
  42. tableView.layer.cornerRadius = 10.0f;
  43. // 定点
  44. tableView.layer.anchorPoint = CGPointMake(1.0, 0);
  45. tableView.transform =CGAffineTransformMakeScale(0.0001, 0.0001);
  46. tableView.rowHeight = 40;
  47. [win addSubview:tableView];
  48. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapBackgroundClick)];
  49. [backgroundView addGestureRecognizer:tap];
  50. backgroundView.action = action;
  51. backgroundView.selectData = select;
  52. // tableView.layer.anchorPoint = CGPointMake(100, 64);
  53. if (animaton == YES) {
  54. backgroundView.alpha = 0;
  55. // tableView.frame = CGRectMake([UIScreen mainScreen].bounds.size.width - 80, 70, frame.size.width, 40 * selectData.count);
  56. [UIView animateWithDuration:0.3 animations:^{
  57. backgroundView.alpha = 0.5;
  58. tableView.transform = CGAffineTransformMakeScale(1.0, 1.0);
  59. }];
  60. }
  61. }
  62. +(void)tapBackgroundClick{
  63. [popMenuView hiden];
  64. }
  65. + (void)hiden
  66. {
  67. if (backgroundView != nil) {
  68. [UIView animateWithDuration:0.3 animations:^{
  69. // UIWindow * win = [[[UIApplication sharedApplication] windows] firstObject];
  70. // tableView.frame = CGRectMake(win.bounds.size.width - 35 , 64, 0, 0);
  71. tableView.transform = CGAffineTransformMakeScale(0.000001, 0.0001);
  72. } completion:^(BOOL finished) {
  73. [backgroundView removeFromSuperview];
  74. [tableView removeFromSuperview];
  75. tableView = nil;
  76. backgroundView = nil;
  77. }];
  78. }
  79. }
  80. //tableviewDatasource
  81. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  82. {
  83. return _selectData.count;
  84. }
  85. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  86. {
  87. static NSString *Identifier = @"PellTableViewSelectIdentifier";
  88. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Identifier];
  89. if (cell == nil) {
  90. cell = [[UITableViewCell alloc] initWithStyle:0 reuseIdentifier:Identifier];
  91. }
  92. cell.imageView.image = [UIImage imageNamed:self.imagesData[indexPath.row]];
  93. cell.textLabel.text = _selectData[indexPath.row];
  94. return cell;
  95. }
  96. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  97. {
  98. if (self.action) {
  99. self.action(indexPath.row);
  100. }
  101. [popMenuView hiden];
  102. }
  103. //绘制三角形
  104. - (void)drawRect:(CGRect)rect {
  105. // Drawing code
  106. // 设置背景色
  107. [[UIColor whiteColor] set];
  108. //拿到当前视图准备好的画板
  109. CGContextRef context = UIGraphicsGetCurrentContext();
  110. //利用path进行绘制三角形
  111. CGContextBeginPath(context);//标记
  112. CGFloat location = [UIScreen mainScreen].bounds.size.width;
  113. CGContextMoveToPoint(context,
  114. location - LeftView - 10, 70);//设置起点
  115. CGContextAddLineToPoint(context,
  116. location - 2*LeftView - 10 , 60);
  117. CGContextAddLineToPoint(context,
  118. location - TopToView * 3 - 10, 70);
  119. CGContextClosePath(context);//路径结束标志,不写默认封闭
  120. [[UIColor whiteColor] setFill]; //设置填充色
  121. [[UIColor whiteColor] setStroke]; //设置边框颜色
  122. CGContextDrawPath(context,
  123. kCGPathFillStroke);//绘制路径path
  124. }
  125. @end