MengBanView.m 9.8 KB

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