// // SelectFiltrateView.m // YouHuiProject // // Created by 小花 on 2018/5/2. // Copyright © 2018年 kuxuan. All rights reserved. // #import "SelectFiltrateView.h" @interface SelectFiltrateView () { UIButton *_lastButton; } @property (nonatomic, strong) UILabel *title; @property (nonatomic, strong) UITextField *minField; @property (nonatomic, strong) UITextField *maxField; @end @implementation SelectFiltrateView - (instancetype)initWithFrame:(CGRect)frame filtrateArr:(NSArray *)arr{ self = [super initWithFrame:frame]; if (self) { self.backgroundColor = [UIColor whiteColor]; [self initSubViews:arr]; } return self; } - (void)initSubViews:(NSArray *)arr { self.title = [[UILabel alloc] initWithFrame:CGRectMake(10, 15, 100, 20)]; self.title.textColor = [UIColor YHColorWithHex:0x333333]; self.title.font = [UIFont systemFontOfSize:13]; self.title.text = @"价格区间"; [self addSubview:self.title]; CGFloat fieldWidth = (self.width-45-10*2)/2; CGFloat fieldHeight = 30.f; self.minField = [[UITextField alloc] initWithFrame:CGRectMake(self.title.left, self.title.bottom+15, fieldWidth, fieldHeight)]; self.minField.placeholder = @"最低价"; self.minField.textColor = [UIColor homeRedColor]; self.minField.font = [UIFont systemFontOfSize:13]; self.minField.textAlignment = NSTextAlignmentCenter; self.minField.layer.cornerRadius = 4; self.minField.backgroundColor = [UIColor YHColorWithHex:0xF4F4F4]; self.minField.keyboardType = UIKeyboardTypeNumberPad; [[self.minField.rac_textSignal filter:^BOOL(NSString *value) { if (value.length > 0) { return YES; }else { return NO; } }] subscribeNext:^(id x) { if ([x boolValue]) { _lastButton.backgroundColor = [UIColor YHColorWithHex:0xF4F4F4]; _lastButton = nil; } }]; [self addSubview:self.minField]; UIView *line = [[UIView alloc] initWithFrame:CGRectMake(self.minField.right+10, 0, 25, 2)]; line.backgroundColor = [UIColor YHColorWithHex:0xF4F4F4]; line.centerY = self.minField.centerY; [self addSubview:line]; self.maxField = [[UITextField alloc] initWithFrame:CGRectMake(line.right+10, 0, fieldWidth, fieldHeight)]; self.maxField.centerY = self.minField.centerY; self.maxField.placeholder = @"最高价"; self.maxField.textColor = [UIColor homeRedColor]; self.maxField.font = [UIFont systemFontOfSize:13]; self.maxField.textAlignment = NSTextAlignmentCenter; self.maxField.layer.cornerRadius = 4; self.maxField.backgroundColor = [UIColor YHColorWithHex:0xF4F4F4]; self.maxField.keyboardType = UIKeyboardTypeNumberPad; [[self.maxField.rac_textSignal filter:^BOOL(NSString *value) { if (value.length > 0) { return YES; }else { return NO; } }] subscribeNext:^(id x) { if ([x boolValue]) { _lastButton.backgroundColor = [UIColor YHColorWithHex:0xF4F4F4]; _lastButton = nil; } }]; [self addSubview:self.maxField]; CGFloat btnWidth = (self.width-40)/3; CGFloat btnHeight = 30.f; CGFloat btnMargin = 10.f; NSMutableArray *titles = [NSMutableArray array]; for (int i = 0; i < arr.count; i++) { NSDictionary *dict = arr[i]; NSString *type = [NSString stringWithFormat:@"%@",dict[@"type"]]; if ([type isEqualToString:@"1"]) { [titles addObject:[NSString stringWithFormat:@"%@-%@",dict[@"min_price"],dict[@"max_price"]]]; }else if([type isEqualToString:@"0"]){ [titles addObject:[NSString stringWithFormat:@"%@以下",dict[@"max_price"]]]; }else if ([type isEqualToString:@"2"]){ [titles addObject:[NSString stringWithFormat:@"%@以上",dict[@"min_price"]]]; } } for (int i =0 ; i < titles.count; i++) { UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(10+(btnWidth+btnMargin)*i, self.minField.bottom+15, btnWidth, btnHeight)]; button.titleLabel.font = [UIFont systemFontOfSize:13]; [button setTitle:titles[i] forState:UIControlStateNormal]; [button setTitleColor:[UIColor YHColorWithHex:0x333333] forState:UIControlStateNormal]; button.backgroundColor = [UIColor YHColorWithHex:0xF4F4F4]; [button addTarget:self action:@selector(selectedButtonAction:) forControlEvents:UIControlEventTouchUpInside]; button.layer.cornerRadius = 4; button.tag = 1000+i; [self addSubview:button]; } self.cancelBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, self.height-47, self.width/2, 47)]; [self.cancelBtn setTitle:@"重置" forState:UIControlStateNormal]; [self.cancelBtn setTitleColor:[UIColor YHColorWithHex:0x333333] forState:UIControlStateNormal]; self.cancelBtn.titleLabel.font = [UIFont systemFontOfSize:15]; self.cancelBtn.backgroundColor = [UIColor YHColorWithHex:0xF4F4F4]; [self.cancelBtn addTarget:self action:@selector(resetButtonAction) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:self.cancelBtn]; self.makeSureBtn = [[UIButton alloc] initWithFrame:CGRectMake(self.cancelBtn.right, self.height-47, self.width/2, 47)]; [self.makeSureBtn setTitle:@"确定" forState:UIControlStateNormal]; self.makeSureBtn.titleLabel.font = [UIFont systemFontOfSize:15]; self.makeSureBtn.backgroundColor = [UIColor homeRedColor]; [self.makeSureBtn addTarget:self action:@selector(makeSureAction:) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:self.makeSureBtn]; } #pragma mark- ----------- - (void)selectedButtonAction:(UIButton *)sender { [self resetTextField]; if (sender != _lastButton) { _lastButton.backgroundColor = [UIColor YHColorWithHex:0xF4F4F4]; sender.backgroundColor = [UIColor homeRedColor]; _lastButton = sender; } } /** 确定 */ - (void)makeSureAction:(UIButton *)sender { NSString *min = @"0"; //起始值 NSString *max = @"-1"; //终止值 if (_lastButton) { NSString *rangeText = _lastButton.titleLabel.text; if ([rangeText containsString:@"-"]) { NSArray *arr = [rangeText componentsSeparatedByString:@"-"]; min = arr.firstObject; max = arr.lastObject; }else if([rangeText containsString:@"以下"]){ NSArray *arr = [rangeText componentsSeparatedByString:@"以下"]; min = @"0"; max = arr.firstObject; }else if([rangeText containsString:@"以上"]){ NSArray *arr = [rangeText componentsSeparatedByString:@"以上"]; min = arr.firstObject; max = @"-1"; } } if (self.minField.text.length > 0 && self.maxField.text.length > 0 && [self.maxField.text integerValue] > [self.minField.text integerValue]) { min = self.minField.text; max = self.maxField.text; }else if([self.maxField.text integerValue] < [self.minField.text integerValue]){ [MBProgressHUD showMessage:@"请输入正确的价格区间"]; return; } if (self.delegate && [self.delegate respondsToSelector:@selector(makeSureActionWithFromValue:toValue:)]) { [self.delegate makeSureActionWithFromValue:min toValue:max]; } [self.minField resignFirstResponder]; [self.maxField resignFirstResponder]; } /** 重置 */ - (void)resetButtonAction { [self resetTextField]; _lastButton.backgroundColor = [UIColor YHColorWithHex:0xF4F4F4]; _lastButton = nil; if (self.delegate && [self.delegate respondsToSelector:@selector(makeSureActionWithFromValue:toValue:)]) { [self.delegate makeSureActionWithFromValue:@"0" toValue:@"-1"]; } } - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { //处理点击空白处收起 } - (void)resetTextField { self.minField.text = nil; self.maxField.text = nil; [self.minField endEditing:YES]; [self.maxField endEditing:YES]; } @end