口袋优选

SelectFiltrateView.m 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. //
  2. // SelectFiltrateView.m
  3. // YouHuiProject
  4. //
  5. // Created by 小花 on 2018/5/2.
  6. // Copyright © 2018年 kuxuan. All rights reserved.
  7. //
  8. #import "SelectFiltrateView.h"
  9. @interface SelectFiltrateView ()
  10. {
  11. UIButton *_lastButton;
  12. }
  13. @property (nonatomic, strong) UILabel *title;
  14. @property (nonatomic, strong) UITextField *minField;
  15. @property (nonatomic, strong) UITextField *maxField;
  16. @end
  17. @implementation SelectFiltrateView
  18. - (instancetype)initWithFrame:(CGRect)frame filtrateArr:(NSArray *)arr{
  19. self = [super initWithFrame:frame];
  20. if (self) {
  21. self.backgroundColor = [UIColor whiteColor];
  22. [self initSubViews:arr];
  23. }
  24. return self;
  25. }
  26. - (void)initSubViews:(NSArray *)arr {
  27. self.title = [[UILabel alloc] initWithFrame:CGRectMake(10, 15, 100, 20)];
  28. self.title.textColor = [UIColor YHColorWithHex:0x333333];
  29. self.title.font = [UIFont systemFontOfSize:13];
  30. self.title.text = @"价格区间";
  31. [self addSubview:self.title];
  32. CGFloat fieldWidth = (self.width-45-10*2)/2;
  33. CGFloat fieldHeight = 30.f;
  34. self.minField = [[UITextField alloc] initWithFrame:CGRectMake(self.title.left, self.title.bottom+15, fieldWidth, fieldHeight)];
  35. self.minField.placeholder = @"最低价";
  36. self.minField.textColor = [UIColor homeRedColor];
  37. self.minField.font = [UIFont systemFontOfSize:13];
  38. self.minField.textAlignment = NSTextAlignmentCenter;
  39. self.minField.layer.cornerRadius = 4;
  40. self.minField.backgroundColor = [UIColor YHColorWithHex:0xF4F4F4];
  41. self.minField.keyboardType = UIKeyboardTypeNumberPad;
  42. [[self.minField.rac_textSignal filter:^BOOL(NSString *value) {
  43. if (value.length > 0) {
  44. return YES;
  45. }else {
  46. return NO;
  47. }
  48. }] subscribeNext:^(id x) {
  49. if ([x boolValue]) {
  50. _lastButton.backgroundColor = [UIColor YHColorWithHex:0xF4F4F4];
  51. _lastButton = nil;
  52. }
  53. }];
  54. [self addSubview:self.minField];
  55. UIView *line = [[UIView alloc] initWithFrame:CGRectMake(self.minField.right+10, 0, 25, 2)];
  56. line.backgroundColor = [UIColor YHColorWithHex:0xF4F4F4];
  57. line.centerY = self.minField.centerY;
  58. [self addSubview:line];
  59. self.maxField = [[UITextField alloc] initWithFrame:CGRectMake(line.right+10, 0, fieldWidth, fieldHeight)];
  60. self.maxField.centerY = self.minField.centerY;
  61. self.maxField.placeholder = @"最高价";
  62. self.maxField.textColor = [UIColor homeRedColor];
  63. self.maxField.font = [UIFont systemFontOfSize:13];
  64. self.maxField.textAlignment = NSTextAlignmentCenter;
  65. self.maxField.layer.cornerRadius = 4;
  66. self.maxField.backgroundColor = [UIColor YHColorWithHex:0xF4F4F4];
  67. self.maxField.keyboardType = UIKeyboardTypeNumberPad;
  68. [[self.maxField.rac_textSignal filter:^BOOL(NSString *value) {
  69. if (value.length > 0) {
  70. return YES;
  71. }else {
  72. return NO;
  73. }
  74. }] subscribeNext:^(id x) {
  75. if ([x boolValue]) {
  76. _lastButton.backgroundColor = [UIColor YHColorWithHex:0xF4F4F4];
  77. _lastButton = nil;
  78. }
  79. }];
  80. [self addSubview:self.maxField];
  81. CGFloat btnWidth = (self.width-40)/3;
  82. CGFloat btnHeight = 30.f;
  83. CGFloat btnMargin = 10.f;
  84. NSMutableArray *titles = [NSMutableArray array];
  85. for (int i = 0; i < arr.count; i++) {
  86. NSDictionary *dict = arr[i];
  87. NSString *type = [NSString stringWithFormat:@"%@",dict[@"type"]];
  88. if ([type isEqualToString:@"1"]) {
  89. [titles addObject:[NSString stringWithFormat:@"%@-%@",dict[@"min_price"],dict[@"max_price"]]];
  90. }else if([type isEqualToString:@"0"]){
  91. [titles addObject:[NSString stringWithFormat:@"%@以下",dict[@"max_price"]]];
  92. }else if ([type isEqualToString:@"2"]){
  93. [titles addObject:[NSString stringWithFormat:@"%@以上",dict[@"min_price"]]];
  94. }
  95. }
  96. for (int i =0 ; i < titles.count; i++) {
  97. UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(10+(btnWidth+btnMargin)*i, self.minField.bottom+15, btnWidth, btnHeight)];
  98. button.titleLabel.font = [UIFont systemFontOfSize:13];
  99. [button setTitle:titles[i] forState:UIControlStateNormal];
  100. [button setTitleColor:[UIColor YHColorWithHex:0x333333] forState:UIControlStateNormal];
  101. button.backgroundColor = [UIColor YHColorWithHex:0xF4F4F4];
  102. [button addTarget:self action:@selector(selectedButtonAction:) forControlEvents:UIControlEventTouchUpInside];
  103. button.layer.cornerRadius = 4;
  104. button.tag = 1000+i;
  105. [self addSubview:button];
  106. }
  107. self.cancelBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, self.height-47, self.width/2, 47)];
  108. [self.cancelBtn setTitle:@"重置" forState:UIControlStateNormal];
  109. [self.cancelBtn setTitleColor:[UIColor YHColorWithHex:0x333333] forState:UIControlStateNormal];
  110. self.cancelBtn.titleLabel.font = [UIFont systemFontOfSize:15];
  111. self.cancelBtn.backgroundColor = [UIColor YHColorWithHex:0xF4F4F4];
  112. [self.cancelBtn addTarget:self action:@selector(resetButtonAction) forControlEvents:UIControlEventTouchUpInside];
  113. [self addSubview:self.cancelBtn];
  114. self.makeSureBtn = [[UIButton alloc] initWithFrame:CGRectMake(self.cancelBtn.right, self.height-47, self.width/2, 47)];
  115. [self.makeSureBtn setTitle:@"确定" forState:UIControlStateNormal];
  116. self.makeSureBtn.titleLabel.font = [UIFont systemFontOfSize:15];
  117. self.makeSureBtn.backgroundColor = [UIColor homeRedColor];
  118. [self.makeSureBtn addTarget:self action:@selector(makeSureAction:) forControlEvents:UIControlEventTouchUpInside];
  119. [self addSubview:self.makeSureBtn];
  120. }
  121. #pragma mark- -----------
  122. - (void)selectedButtonAction:(UIButton *)sender {
  123. [self resetTextField];
  124. if (sender != _lastButton) {
  125. _lastButton.backgroundColor = [UIColor YHColorWithHex:0xF4F4F4];
  126. sender.backgroundColor = [UIColor homeRedColor];
  127. _lastButton = sender;
  128. }
  129. }
  130. /**
  131. 确定
  132. */
  133. - (void)makeSureAction:(UIButton *)sender {
  134. NSString *min = @"0"; //起始值
  135. NSString *max = @"-1"; //终止值
  136. if (_lastButton) {
  137. NSString *rangeText = _lastButton.titleLabel.text;
  138. if ([rangeText containsString:@"-"]) {
  139. NSArray *arr = [rangeText componentsSeparatedByString:@"-"];
  140. min = arr.firstObject;
  141. max = arr.lastObject;
  142. }else if([rangeText containsString:@"以下"]){
  143. NSArray *arr = [rangeText componentsSeparatedByString:@"以下"];
  144. min = @"0";
  145. max = arr.firstObject;
  146. }else if([rangeText containsString:@"以上"]){
  147. NSArray *arr = [rangeText componentsSeparatedByString:@"以上"];
  148. min = arr.firstObject;
  149. max = @"-1";
  150. }
  151. }
  152. if (self.minField.text.length > 0 && self.maxField.text.length > 0 && [self.maxField.text integerValue] > [self.minField.text integerValue]) {
  153. min = self.minField.text;
  154. max = self.maxField.text;
  155. }else if([self.maxField.text integerValue] < [self.minField.text integerValue]){
  156. [MBProgressHUD showMessage:@"请输入正确的价格区间"];
  157. return;
  158. }
  159. if (self.delegate && [self.delegate respondsToSelector:@selector(makeSureActionWithFromValue:toValue:)]) {
  160. [self.delegate makeSureActionWithFromValue:min toValue:max];
  161. }
  162. [self.minField resignFirstResponder];
  163. [self.maxField resignFirstResponder];
  164. }
  165. /**
  166. 重置
  167. */
  168. - (void)resetButtonAction {
  169. [self resetTextField];
  170. _lastButton.backgroundColor = [UIColor YHColorWithHex:0xF4F4F4];
  171. _lastButton = nil;
  172. if (self.delegate && [self.delegate respondsToSelector:@selector(makeSureActionWithFromValue:toValue:)]) {
  173. [self.delegate makeSureActionWithFromValue:@"0" toValue:@"-1"];
  174. }
  175. }
  176. - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
  177. //处理点击空白处收起
  178. }
  179. - (void)resetTextField {
  180. self.minField.text = nil;
  181. self.maxField.text = nil;
  182. [self.minField endEditing:YES];
  183. [self.maxField endEditing:YES];
  184. }
  185. @end