MengBanView.m 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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. #import "DateView.h"
  11. #define BtnHeight 40
  12. #define Vertical_Space 20
  13. #define KWINDOW [UIApplication sharedApplication].keyWindow
  14. #define CGCOLOR_LINE [UIColor colorWithRed:136/255.0f green:136/255.0f blue:136/255.0f alpha:1].CGColor
  15. @interface MengBanView ()<UITextFieldDelegate>
  16. {
  17. BOOL _isAllbtn;
  18. MBContentView * _contentView; //对点击响应做了处理(超出contentview部分的点击)
  19. NSString * _title; //标题
  20. NSArray *_btnsArr; //所有供选择btn的内容
  21. NSInteger _count; //记录_contentView基本子视图个数(判断是否已经有待选择view)
  22. UIButton *_selectBtn; //记录当前待选择的btn
  23. NSMutableArray *_originalSelectArr; //show之前的所有btn初始值
  24. MBBlock _block; //确定btn的回调
  25. NSMutableArray *_selectArr; //确定回调的内容
  26. }
  27. @end
  28. @implementation MengBanView
  29. #pragma mark 初始化方法
  30. -(instancetype)initWithTitileStr:(NSString *)title buttonsArray:(NSArray *)btnsArr isAllbtn:(BOOL)isAllbtn block:(MBBlock)block{
  31. self = [super init];
  32. if (self) {
  33. self.frame = KWINDOW.bounds;
  34. _title = title;
  35. _btnsArr = btnsArr;
  36. _isAllbtn = isAllbtn;
  37. _count = 0;
  38. _block = block;
  39. _selectArr = [NSMutableArray new];
  40. if (isAllbtn) {
  41. [self layoutAllSubviews];//清一色button
  42. for (NSArray * arr in btnsArr) {
  43. [_selectArr addObject:arr[0]];
  44. }
  45. }else{
  46. [self layoutDifferentSubviews];//大杂烩
  47. for (NSArray * arr in btnsArr) {
  48. if ([arr[0] isEqualToString:@"1"]) {
  49. [_selectArr addObject:arr[1][0]];
  50. }else if([arr[0] isEqualToString:@"2"]){
  51. [_selectArr addObject:arr[1]];
  52. }else if([arr[0] isEqualToString:@"3"]){
  53. [_selectArr addObject:@""];
  54. }
  55. }
  56. }
  57. }
  58. return self;
  59. }
  60. #pragma mark 布局
  61. -(void)layoutAllSubviews{
  62. /*创建灰色背景*/
  63. UIView *bgView = [[UIView alloc] initWithFrame:KWINDOW.bounds];
  64. bgView.backgroundColor = [UIColor colorWithWhite:.01 alpha:.4];
  65. [self addSubview:bgView];
  66. /*添加手势事件,移除View*/
  67. UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissContactView:)];
  68. [bgView addGestureRecognizer:tapGesture];
  69. /*创建显示View*/
  70. NSInteger k = 0;
  71. if (_title.length != 0) {
  72. k++;
  73. }
  74. k += _btnsArr.count+1;//+1 是因为取消,确定
  75. _contentView = [[MBContentView alloc] initWithFrame:CGRectMake(0, 0, kSize.width*280/320, (BtnHeight+Vertical_Space)*k+Vertical_Space)];//多加一个间隔也是给取消确定用的(ps:有标题的情况高度就不作处理了 这样上下距离感还好)
  76. _contentView.center = self.center;
  77. _contentView.backgroundColor=RGB_COLOR(246, 246, 246);
  78. _contentView.alpha = 1.0;
  79. _contentView.layer.cornerRadius = 10;
  80. // _contentView.layer.masksToBounds = YES;
  81. [self addSubview:_contentView];
  82. CGSize contentSize = _contentView.frame.size;
  83. CGFloat x = 20;
  84. CGFloat y = Vertical_Space;
  85. CGFloat w = contentSize.width-2*20;
  86. CGFloat h = BtnHeight;
  87. if (_title.length != 0) {
  88. UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(x, y, w, h)];
  89. label.text = _title;
  90. label.textAlignment = NSTextAlignmentCenter;
  91. label.textColor = [UIColor blackColor];
  92. label.font = [UIFont fontWithName:@"Helvetica-Bold" size:17];
  93. [_contentView addSubview:label];
  94. _count++;
  95. //
  96. y += BtnHeight;
  97. }
  98. for (int i=0; i<_btnsArr.count; i++) {
  99. UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(x, y, w, h)];
  100. button.backgroundColor = [UIColor whiteColor];
  101. button.layer.cornerRadius = 4;
  102. button.layer.borderWidth = .5f;
  103. button.layer.borderColor = CGCOLOR_LINE;
  104. button.titleLabel.font = [UIFont systemFontOfSize:15];
  105. [button setTitle:_btnsArr[i][0] forState:UIControlStateNormal];
  106. [button setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
  107. // button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
  108. button.tag=100+i;
  109. [button addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
  110. [_contentView addSubview:button];
  111. _count++;
  112. if (i != _btnsArr.count-1) {
  113. y += Vertical_Space+BtnHeight;
  114. }
  115. }
  116. x = 40;
  117. y += 30+BtnHeight;
  118. w = (contentSize.width-40*3)/2;
  119. UIButton *button3 = [[UIButton alloc]initWithFrame:CGRectMake(x, y, w, h)];
  120. button3.layer.cornerRadius = 4;
  121. button3.layer.borderWidth = .5f;
  122. button3.layer.borderColor = CGCOLOR_LINE;
  123. [button3 setTitle:@"取消" forState:UIControlStateNormal];
  124. [button3 setTitleColor:RQMianColor forState:UIControlStateNormal];
  125. button3.tag = 10;
  126. [button3 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
  127. [_contentView addSubview:button3];
  128. _count++;
  129. x = 40*2+w;
  130. UIButton *button4 = [[UIButton alloc]initWithFrame:CGRectMake(x, y, w, h)];
  131. button4.layer.cornerRadius = 4;
  132. button4.layer.borderWidth = .5f;
  133. button4.layer.borderColor = CGCOLOR_LINE;
  134. [button4 setTitle:@"确定" forState:UIControlStateNormal];
  135. [button4 setTitleColor:RQMianColor forState:UIControlStateNormal];
  136. button4.tag = 11;
  137. [button4 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
  138. [_contentView addSubview:button4];
  139. _count++;
  140. }
  141. -(void)layoutDifferentSubviews{
  142. /*创建灰色背景*/
  143. UIView *bgView = [[UIView alloc] initWithFrame:KWINDOW.bounds];
  144. bgView.backgroundColor = [UIColor colorWithWhite:.01 alpha:.4];
  145. [self addSubview:bgView];
  146. /*添加手势事件,移除View*/
  147. UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissContactView:)];
  148. [bgView addGestureRecognizer:tapGesture];
  149. /*创建显示View*/
  150. NSInteger k = 0;
  151. if (_title.length != 0) {
  152. k++;
  153. }
  154. k += _btnsArr.count+1;//+1 是因为取消,确定
  155. _contentView = [[MBContentView alloc] initWithFrame:CGRectMake(0, 0, kSize.width*280/320, (BtnHeight+Vertical_Space)*k+Vertical_Space)];//多加一个间隔也是给取消确定用的(ps:有标题的情况高度就不作处理了 这样上下距离感还好)
  156. _contentView.center = self.center;
  157. _contentView.backgroundColor=RGB_COLOR(246, 246, 246);
  158. _contentView.alpha = 1.0;
  159. _contentView.layer.cornerRadius = 10;
  160. // _contentView.layer.masksToBounds = YES;
  161. [self addSubview:_contentView];
  162. CGSize contentSize = _contentView.frame.size;
  163. CGFloat x = 20;
  164. CGFloat y = Vertical_Space;
  165. CGFloat w = contentSize.width-2*20;
  166. CGFloat h = BtnHeight;
  167. if (_title.length != 0) {
  168. UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(x, y, w, h)];
  169. label.text = _title;
  170. label.textAlignment = NSTextAlignmentCenter;
  171. label.textColor = [UIColor blackColor];
  172. label.font = [UIFont fontWithName:@"Helvetica-Bold" size:17];
  173. [_contentView addSubview:label];
  174. _count++;
  175. //
  176. y += BtnHeight;
  177. }
  178. int i=0;
  179. for (NSArray * obj in _btnsArr) {
  180. //1button 2selcdate 3textfiled,
  181. if ([obj[0] isEqualToString:@"3"]) {
  182. UITextField * tf = obj[1];
  183. tf.frame = CGRectMake(x, y, w, h);
  184. tf.backgroundColor = [UIColor whiteColor];
  185. tf.delegate = self;
  186. tf.tag = 1000+i;
  187. [_contentView addSubview:tf];
  188. }else{
  189. UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(x, y, w, h)];
  190. button.backgroundColor = [UIColor whiteColor];
  191. button.layer.cornerRadius = 4;
  192. button.layer.borderWidth = .5f;
  193. button.layer.borderColor = CGCOLOR_LINE;
  194. button.titleLabel.font = [UIFont systemFontOfSize:15];
  195. id theObj = _btnsArr[i][1];
  196. if ([obj[0] isEqualToString:@"2"]) {
  197. [button setTitle:theObj forState:UIControlStateNormal];
  198. button.tag=4399+i;//日期选择
  199. }else{
  200. [button setTitle:theObj[0] forState:UIControlStateNormal];
  201. button.tag=1+i;
  202. }
  203. [button setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
  204. // button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
  205. [button addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
  206. [_contentView addSubview:button];
  207. }
  208. _count++;
  209. if (i != _btnsArr.count-1) {
  210. y += Vertical_Space+BtnHeight;
  211. }
  212. i++;
  213. }
  214. x = 40;
  215. y += 30+BtnHeight;
  216. w = (contentSize.width-40*3)/2;
  217. UIButton *button3 = [[UIButton alloc]initWithFrame:CGRectMake(x, y, w, h)];
  218. button3.layer.cornerRadius = 4;
  219. button3.layer.borderWidth = .5f;
  220. button3.layer.borderColor = CGCOLOR_LINE;
  221. [button3 setTitle:@"取消" forState:UIControlStateNormal];
  222. [button3 setTitleColor:RQMianColor forState:UIControlStateNormal];
  223. button3.tag = 10;
  224. [button3 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
  225. [_contentView addSubview:button3];
  226. _count++;
  227. x = 40*2+w;
  228. UIButton *button4 = [[UIButton alloc]initWithFrame:CGRectMake(x, y, w, h)];
  229. button4.layer.cornerRadius = 4;
  230. button4.layer.borderWidth = .5f;
  231. button4.layer.borderColor = CGCOLOR_LINE;
  232. [button4 setTitle:@"确定" forState:UIControlStateNormal];
  233. [button4 setTitleColor:RQMianColor forState:UIControlStateNormal];
  234. button4.tag = 11;
  235. [button4 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
  236. [_contentView addSubview:button4];
  237. _count++;
  238. }
  239. #pragma mark 点击事件
  240. -(void)btnClick:(UIButton *)sender{
  241. for (id obj in _contentView.subviews) {
  242. if ([obj isKindOfClass:[UITextField class]] && [obj isFirstResponder]) {
  243. [obj resignFirstResponder];
  244. }
  245. }
  246. switch (sender.tag) {
  247. case 10:
  248. {
  249. //取消
  250. [self dismissContactViewWithIsSure:NO];
  251. }
  252. break;
  253. case 11:
  254. {
  255. //确定
  256. [self dismissContactViewWithIsSure:YES];
  257. if (_block) {
  258. _block(_selectArr);
  259. }
  260. }
  261. break;
  262. default:
  263. {
  264. if (sender.tag >= 4399) {
  265. //选择日期
  266. [self creatDateView];
  267. }else{
  268. NSArray * titles;
  269. if (sender.tag < 10) {
  270. //deifferent
  271. titles = _btnsArr[sender.tag-1][1];
  272. }else{
  273. //All button
  274. titles = _btnsArr[sender.tag-100];
  275. }
  276. [self creatMenuWithTitles:titles Frame:sender.frame];
  277. }
  278. _selectBtn = sender;
  279. }
  280. break;
  281. }
  282. }
  283. -(void)creatDateView{
  284. if (_contentView.subviews.count > _count) {
  285. [[_contentView.subviews lastObject] removeFromSuperview];
  286. }
  287. DateView *dateV = [[DateView alloc] init];
  288. [dateV setStyle:0];
  289. [dateV showWithComplete:^(NSString * result) {
  290. //更新UI
  291. [_selectBtn setTitle:result forState:UIControlStateNormal];
  292. //更新返回内容
  293. [_selectArr replaceObjectAtIndex:_selectBtn.tag-4399 withObject:result];
  294. }];
  295. }
  296. -(void)creatMenuWithTitles:(NSArray *)titles Frame:(CGRect)frame{
  297. if (_contentView.subviews.count > _count) {
  298. [[_contentView.subviews lastObject] removeFromSuperview];
  299. }
  300. CGFloat height = 44;
  301. UIView * view = [[UIView alloc]initWithFrame:CGRectMake(frame.origin.x+10, frame.origin.y, frame.size.width-20, height*titles.count)];
  302. view.backgroundColor = RGB_COLOR(231, 231, 231);
  303. view.alpha = 1.0;
  304. for (int i=0; i<titles.count; i++) {
  305. UIButton * button = [UIButton buttonWithType:UIButtonTypeSystem];
  306. button.frame = CGRectMake(0, i*height, frame.size.width-20, height);
  307. button.contentMode = UIViewContentModeLeft;
  308. [button setTitle:titles[i] forState:UIControlStateNormal];
  309. [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  310. button.tag = 1000+i;
  311. [button addTarget:self action:@selector(selectBtnClick:) forControlEvents:UIControlEventTouchUpInside];
  312. [view addSubview:button];
  313. }
  314. [_contentView addSubview:view];
  315. }
  316. -(void)selectBtnClick:(UIButton *)sender{
  317. //更新UI
  318. [_selectBtn setTitle:sender.currentTitle forState:UIControlStateNormal];
  319. [self dismissContactViewWithIsSure:NO];
  320. //更新返回内容
  321. if (_selectBtn.tag >= 100) {
  322. [_selectArr replaceObjectAtIndex:_selectBtn.tag-100 withObject:sender.currentTitle];
  323. }else{
  324. [_selectArr replaceObjectAtIndex:_selectBtn.tag-1 withObject:sender.currentTitle];
  325. }
  326. }
  327. #pragma mark UITextFiled代理
  328. - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
  329. NSString *newStr = [textField.text stringByReplacingCharactersInRange:range withString:string];
  330. [_selectArr replaceObjectAtIndex:textField.tag-1000 withObject:newStr];
  331. return YES;
  332. }
  333. #pragma mark 自定义内容视图Frame:(CGRect)frame
  334. -(instancetype)initWithContentView:(UIView *)contentView{
  335. if (self = [super init]) {
  336. self.frame = KWINDOW.bounds;
  337. /*创建灰色背景*/
  338. UIView *bgView = [[UIView alloc] initWithFrame:KWINDOW.bounds];
  339. bgView.backgroundColor = [UIColor colorWithWhite:.01 alpha:.4];
  340. [self addSubview:bgView];
  341. /*添加手势事件,移除View*/
  342. UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissContactView:)];
  343. [bgView addGestureRecognizer:tapGesture];
  344. //添加传入的内容视图
  345. [self addSubview:contentView];
  346. }
  347. return self;
  348. }
  349. #pragma mark - 手势点击事件,移除View
  350. - (void)dismissContactView:(UITapGestureRecognizer *)tapGesture{
  351. //如果有键盘弹出的话,就隐藏键盘好了
  352. for (id obj in _contentView.subviews) {
  353. if ([obj isKindOfClass:[UITextField class]] && [obj isFirstResponder]) {
  354. [obj resignFirstResponder];
  355. return;
  356. }
  357. }
  358. [self dismissContactViewWithIsSure:NO];
  359. }
  360. -(void)dismissContactViewWithIsSure:(BOOL)sure
  361. {
  362. if (_contentView.subviews.count > _count) {
  363. [[_contentView.subviews lastObject] removeFromSuperview];
  364. return;
  365. }
  366. //如果是取消
  367. if (!sure) {
  368. //取消(取消之前恢复按钮原有选取)
  369. _selectArr = [_originalSelectArr mutableCopy];
  370. //判断是否全是button
  371. if (_isAllbtn) {
  372. for (int i=0; i<_originalSelectArr.count; i++) {
  373. UIButton *btn = (UIButton *)[_contentView viewWithTag:100+i];
  374. [btn setTitle:_originalSelectArr[i] forState:UIControlStateNormal];
  375. }
  376. }else{
  377. for (int i=0; i<_originalSelectArr.count; i++) {
  378. if ([_btnsArr[i][0] isEqualToString:@"1"]) {
  379. UIButton *btn = (UIButton *)[_contentView viewWithTag:1+i];
  380. [btn setTitle:_originalSelectArr[i] forState:UIControlStateNormal];
  381. }else if ([_btnsArr[i][0] isEqualToString:@"2"]){
  382. //date
  383. UIButton *btn = (UIButton *)[_contentView viewWithTag:4399+i];
  384. [btn setTitle:_originalSelectArr[i] forState:UIControlStateNormal];
  385. }else if ([_btnsArr[i][0] isEqualToString:@"3"]){
  386. //textfiled
  387. UITextField * tf = (UITextField *)[_contentView viewWithTag:1000+i];
  388. tf.text = _originalSelectArr[i];
  389. }
  390. }
  391. }
  392. }
  393. __weak typeof(self)weakSelf = self;
  394. [UIView animateWithDuration:0.5 animations:^{
  395. weakSelf.alpha = 0;
  396. } completion:^(BOOL finished) {
  397. [weakSelf removeFromSuperview];
  398. }];
  399. }
  400. // 这里加载在了window上
  401. -(void)showView
  402. {
  403. _originalSelectArr = [_selectArr mutableCopy];//记录按钮初始选择,用以点击取消的时候恢复初始选择
  404. self.alpha = 1.0;
  405. UIWindow * window = myDelegate.window;
  406. [window addSubview:self];
  407. }
  408. @end