MengBanView.m 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. //
  2. // MengBanView.m
  3. // LNManager
  4. //
  5. // Created by EchoShacolee on 2017/4/11.
  6. // Copyright © 2017年 lee. All rights reserved.
  7. //
  8. #import "MengBanView.h"
  9. #import "MBContentView.h"
  10. #define BtnHeight 40
  11. #define Vertical_Space 20
  12. @interface MengBanView ()
  13. {
  14. MBContentView * _contentView; //对点击响应做了处理(超出contentview部分的点击)
  15. NSString * _title; //标题
  16. NSArray *_btnsArr; //所有供选择btn的内容
  17. NSInteger _count; //记录_contentView基本子视图个数(判断是否已经有待选择view)
  18. UIButton *_selectBtn; //记录当前待选择的btn
  19. NSMutableArray *_originalSelectArr; //show之前的所有btn初始值
  20. MBBlock _block; //确定btn的回调
  21. NSMutableArray *_selectArr; //确定回调的内容
  22. }
  23. @end
  24. @implementation MengBanView
  25. #pragma mark 选择框
  26. -(instancetype)initWithTitileStr:(NSString *)title buttonsArray:(NSArray *)btnsArr block:(MBBlock)block{
  27. self = [super init];
  28. if (self) {
  29. self.frame = KWINDOW.bounds;
  30. _title = title;
  31. _btnsArr = btnsArr;
  32. _count = 0;
  33. _block = block;
  34. _selectArr = [NSMutableArray new];
  35. for (NSArray * arr in btnsArr) {
  36. [_selectArr addObject:arr[0]];
  37. }
  38. [self layoutAllSubviews];
  39. }
  40. return self;
  41. }
  42. -(void)layoutAllSubviews{
  43. /*创建灰色背景*/
  44. UIView *bgView = [[UIView alloc] initWithFrame:KWINDOW.bounds];
  45. bgView.backgroundColor = [UIColor colorWithWhite:.01 alpha:.4];
  46. [self addSubview:bgView];
  47. /*添加手势事件,移除View*/
  48. UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissContactView:)];
  49. [bgView addGestureRecognizer:tapGesture];
  50. /*创建显示View*/
  51. NSInteger k = 0;
  52. if (_title.length != 0) {
  53. k++;
  54. }
  55. k += _btnsArr.count+1;//+1 是因为取消,确定
  56. _contentView = [[MBContentView alloc] initWithFrame:CGRectMake(0, 0, kSize.width*280/320, (BtnHeight+Vertical_Space)*k+Vertical_Space)];//多加一个间隔也是给取消确定用的(ps:有标题的情况高度就不作处理了 这样上下距离感还好)
  57. _contentView.center = self.center;
  58. _contentView.backgroundColor=RGB_COLOR(246, 246, 246);
  59. _contentView.alpha = 1.0;
  60. _contentView.layer.cornerRadius = 10;
  61. // _contentView.layer.masksToBounds = YES;
  62. [self addSubview:_contentView];
  63. CGSize contentSize = _contentView.frame.size;
  64. CGFloat x = 20;
  65. CGFloat y = Vertical_Space;
  66. CGFloat w = contentSize.width-2*20;
  67. CGFloat h = BtnHeight;
  68. if (_title.length != 0) {
  69. UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(x, y, w, h)];
  70. label.text = _title;
  71. label.textAlignment = NSTextAlignmentCenter;
  72. label.textColor = [UIColor blackColor];
  73. label.font = [UIFont fontWithName:@"Helvetica-Bold" size:17];
  74. [_contentView addSubview:label];
  75. _count++;
  76. //
  77. y += BtnHeight;
  78. }
  79. for (int i=0; i<_btnsArr.count; i++) {
  80. UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(x, y, w, h)];
  81. button.backgroundColor = [UIColor whiteColor];
  82. button.layer.cornerRadius = 4;
  83. button.layer.borderWidth = .5f;
  84. button.layer.borderColor = CGCOLOR_LINE;
  85. button.titleLabel.font = [UIFont systemFontOfSize:15];
  86. [button setTitle:_btnsArr[i][0] forState:UIControlStateNormal];
  87. [button setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
  88. // button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
  89. button.tag=100+i;
  90. [button addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
  91. [_contentView addSubview:button];
  92. _count++;
  93. if (i != _btnsArr.count-1) {
  94. y += Vertical_Space+BtnHeight;
  95. }
  96. }
  97. x = 40;
  98. y += 30+BtnHeight;
  99. w = (contentSize.width-40*3)/2;
  100. UIButton *button3 = [[UIButton alloc]initWithFrame:CGRectMake(x, y, w, h)];
  101. button3.layer.cornerRadius = 4;
  102. button3.layer.borderWidth = .5f;
  103. button3.layer.borderColor = CGCOLOR_LINE;
  104. [button3 setTitle:@"取消" forState:UIControlStateNormal];
  105. [button3 setTitleColor:COLOR_THEME forState:UIControlStateNormal];
  106. button3.tag = 10;
  107. [button3 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
  108. [_contentView addSubview:button3];
  109. _count++;
  110. x = 40*2+w;
  111. UIButton *button4 = [[UIButton alloc]initWithFrame:CGRectMake(x, y, w, h)];
  112. button4.layer.cornerRadius = 4;
  113. button4.layer.borderWidth = .5f;
  114. button4.layer.borderColor = CGCOLOR_LINE;
  115. [button4 setTitle:@"确定" forState:UIControlStateNormal];
  116. [button4 setTitleColor:COLOR_THEME forState:UIControlStateNormal];
  117. button4.tag = 11;
  118. [button4 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
  119. [_contentView addSubview:button4];
  120. _count++;
  121. }
  122. -(void)btnClick:(UIButton *)sender{
  123. switch (sender.tag) {
  124. case 10:
  125. {
  126. [self dismissContactViewWithIsSure:NO];
  127. }
  128. break;
  129. case 11:
  130. {
  131. //确定
  132. [self dismissContactViewWithIsSure:YES];
  133. if (_block) {
  134. _block(_selectArr);
  135. }
  136. }
  137. break;
  138. default:
  139. {
  140. NSArray * titles = _btnsArr[sender.tag-100];
  141. [self creatMenuWithTitles:titles Frame:sender.frame];
  142. _selectBtn = sender;
  143. }
  144. break;
  145. }
  146. }
  147. -(void)creatMenuWithTitles:(NSArray *)titles Frame:(CGRect)frame{
  148. if (_contentView.subviews.count > _count) {
  149. [[_contentView.subviews lastObject] removeFromSuperview];
  150. }
  151. CGFloat height = 44;
  152. UIView * view = [[UIView alloc]initWithFrame:CGRectMake(frame.origin.x+10, frame.origin.y, frame.size.width-20, height*titles.count)];
  153. view.backgroundColor = RGB_COLOR(231, 231, 231);
  154. view.alpha = 1.0;
  155. for (int i=0; i<titles.count; i++) {
  156. UIButton * button = [UIButton buttonWithType:UIButtonTypeSystem];
  157. button.frame = CGRectMake(0, i*height, frame.size.width-20, height);
  158. button.contentMode = UIViewContentModeLeft;
  159. [button setTitle:titles[i] forState:UIControlStateNormal];
  160. [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  161. button.tag = 1000+i;
  162. [button addTarget:self action:@selector(selectBtnClick:) forControlEvents:UIControlEventTouchUpInside];
  163. [view addSubview:button];
  164. }
  165. [_contentView addSubview:view];
  166. }
  167. -(void)selectBtnClick:(UIButton *)sender{
  168. //更新UI
  169. [_selectBtn setTitle:sender.currentTitle forState:UIControlStateNormal];
  170. [self dismissContactViewWithIsSure:NO];
  171. //更新返回内容
  172. [_selectArr replaceObjectAtIndex:_selectBtn.tag-100 withObject:sender.currentTitle];
  173. }
  174. #pragma mark 自定义内容视图Frame:(CGRect)frame
  175. -(instancetype)initWithContentView:(UIView *)contentView{
  176. if (self = [super init]) {
  177. self.frame = KWINDOW.bounds;
  178. /*创建灰色背景*/
  179. UIView *bgView = [[UIView alloc] initWithFrame:KWINDOW.bounds];
  180. bgView.backgroundColor = [UIColor colorWithWhite:.01 alpha:.4];
  181. [self addSubview:bgView];
  182. /*添加手势事件,移除View*/
  183. UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissContactView:)];
  184. [bgView addGestureRecognizer:tapGesture];
  185. //添加传入的内容视图
  186. [self addSubview:contentView];
  187. }
  188. return self;
  189. }
  190. #pragma mark - 手势点击事件,移除View
  191. - (void)dismissContactView:(UITapGestureRecognizer *)tapGesture{
  192. [self dismissContactViewWithIsSure:NO];
  193. }
  194. -(void)dismissContactViewWithIsSure:(BOOL)sure
  195. {
  196. if (_contentView.subviews.count > _count) {
  197. [[_contentView.subviews lastObject] removeFromSuperview];
  198. return;
  199. }
  200. //如果是取消
  201. if (!sure) {
  202. //取消(取消之前恢复按钮原有选取)
  203. _selectArr = [_originalSelectArr mutableCopy];
  204. for (int i=0; i<_originalSelectArr.count; i++) {
  205. UIButton *btn = (UIButton *)[_contentView viewWithTag:100+i];
  206. [btn setTitle:_originalSelectArr[i] forState:UIControlStateNormal];
  207. }
  208. }
  209. __weak typeof(self)weakSelf = self;
  210. [UIView animateWithDuration:0.5 animations:^{
  211. weakSelf.alpha = 0;
  212. } completion:^(BOOL finished) {
  213. [weakSelf removeFromSuperview];
  214. }];
  215. }
  216. // 这里加载在了window上
  217. -(void)showView
  218. {
  219. _originalSelectArr = [_selectArr mutableCopy];//记录按钮初始选择,用以点击取消的时候恢复初始选择
  220. self.alpha = 1.0;
  221. UIWindow * window = [UIApplication sharedApplication].windows[0];
  222. [window addSubview:self];
  223. }
  224. @end