// // KDPScreenViewController.m // KuDianProject // // Created by admin on 2019/7/8. // Copyright © 2019 KDP. All rights reserved. // #import "KDPScreenViewController.h" #import "KDPScreenBtnView.h" #define kBtnMargin 14 #define kBaseBtnViewTag 2000 @interface KDPScreenViewController () @property (nonatomic, strong) UILabel *tipLabel; @property (nonatomic, assign) BOOL shouldClear; @end @implementation KDPScreenViewController { BOOL _isPrice; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.navBar.hidden = YES; self.shouldClear = NO; [self setContentView]; } #pragma init - (instancetype)initWithPrice:(BOOL)price{ if (self = [super init]) { _isPrice = price; } return self; } - (void)setContentView{ self.tipLabel = [[UILabel alloc] initWithFrame:CGRectMake(13 + SCREEN_WIDTH/3, KDStatusHeight + 16, SCREEN_WIDTH-91-13, 20)]; self.tipLabel.textColor = [UIColor colorWithHexString:@"0x333333"]; self.tipLabel.font = [UIFont fontWithName:@"PingFang-SC-Medium" size: 14]; NSString *tipText = _isPrice ? @"价格区间(元)":@"利润区间"; self.tipLabel.text = tipText; [self.view addSubview:self.tipLabel]; // 初始化tagView CGFloat btnWidth = (self.view.width - SCREEN_WIDTH/3 - kBtnMargin * 2 - 20)/2; CGFloat height = 45; NSArray *btnDataArray = _isPrice ? @[@"最低价",@"最高价",@"10元以下",@"10-50元",@"50-100元",@"100元以上"] : @[@"最低利润",@"",@"20%以上",@"30%以上",@"40%以上",@"50%以上"]; for (NSInteger i = 0; i < 6; i++) { NSInteger row = i / 2; NSInteger line = i % 2; KDPScreenBtnView *btnView = [[KDPScreenBtnView alloc] initWithFrame:CGRectMake(kBtnMargin +SCREEN_WIDTH/3 + (btnWidth+20)*line,self.tipLabel.bottom + 25 + row *(17+height) , btnWidth, height)]; btnView.delegate = self; btnView.tag = kBaseBtnViewTag + i; btnView.textColor = [UIColor colorWithHexString:@"#333333"]; if (i < 2) { btnView.placeHolderText = btnDataArray[i]; btnView.isEditing = YES; } else{ btnView.text = btnDataArray[i]; btnView.isEditing = NO; UITapGestureRecognizer *tapGest = [[UITapGestureRecognizer alloc] init]; tapGest.numberOfTapsRequired = 1; [tapGest addTarget:self action:@selector(tapGestAction:)]; [btnView addGestureRecognizer:tapGest]; } [self.view addSubview:btnView]; if (i == 1 && _isPrice == NO) { btnView.hidden = YES; } } // 重置按钮和确定按钮 UIButton *resetBtn = [UIButton buttonWithType:UIButtonTypeCustom]; resetBtn.frame = CGRectMake(SCREEN_WIDTH/3 + 14, self.tipLabel.bottom + 235, (SCREEN_WIDTH-SCREEN_WIDTH/3-28)/2, 41); // [resetBtn setGradientBackgroundWithColors:@[[UIColor colorWithRGB:0xFFC300],[UIColor colorWithRGB:0xFF9A01]] locations:@[@0,@1] startPoint:CGPointMake(0, 0) endPoint:CGPointMake(0, 1)]; resetBtn.backgroundColor = [UIColor colorWithHex:0xFF7693]; [resetBtn addCornerRadiusByRoundingCorners:UIRectCornerTopLeft | UIRectCornerBottomLeft cornerRadii:CGSizeMake(20, 20)]; resetBtn.showsTouchWhenHighlighted = NO; [resetBtn setTitle:@"重置" forState:UIControlStateNormal]; [resetBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; resetBtn.titleLabel.font = FONT_SYS(14); [resetBtn addTarget:self action:@selector(resetAction:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:resetBtn]; UIButton *confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom]; confirmBtn.frame = CGRectMake(resetBtn.right, self.tipLabel.bottom + 235, (SCREEN_WIDTH-SCREEN_WIDTH/3-28)/2, 41); // [confirmBtn setGradientBackgroundWithColors:@[[UIColor colorWithRGB:0xFF8300],[UIColor colorWithRGB:0xFF5200]] locations:@[@0,@1] startPoint:CGPointMake(0, 0) endPoint:CGPointMake(0, 1)]; confirmBtn.backgroundColor = [UIColor colorWithHex:0xFF6B8A]; [confirmBtn addCornerRadiusByRoundingCorners:UIRectCornerTopRight | UIRectCornerBottomRight cornerRadii:CGSizeMake(20, 20)]; confirmBtn.showsTouchWhenHighlighted = NO; [confirmBtn setTitle:@"确定" forState:UIControlStateNormal]; [confirmBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; confirmBtn.titleLabel.font = FONT_SYS(14); [confirmBtn addTarget:self action:@selector(confirmAction:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:confirmBtn]; } #pragma mark event handle - (void)tapGestAction:(UITapGestureRecognizer *)tap{ [self resetBtnView]; KDPScreenBtnView *btnView = (KDPScreenBtnView *)tap.view; btnView.textColor = [UIColor baseColor]; self.shouldClear = NO; [self setUpDataWithSelectTag:btnView.tag]; } - (void)resetBtnView{ [self.view.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { if ([obj isKindOfClass:[KDPScreenBtnView class]]) { KDPScreenBtnView *btnView = (KDPScreenBtnView *)obj; if (btnView.tag > kBaseBtnViewTag + 1) { btnView.textColor = [UIColor colorWithHex:0x333333]; } } }]; } - (void)setUpDataWithSelectTag:(NSInteger )btnTag{ if (_isPrice == YES) { [self setUpPriceTextWithTag:btnTag]; } else{ [self setUpProfitTextWithTag:btnTag]; } } - (void)setUpPriceTextWithTag:(NSInteger)btnTag{ NSString *mineString = @""; NSString *maxString = @""; if (btnTag == kBaseBtnViewTag + 2) { mineString = @"0"; maxString = @"10"; } else if (btnTag == kBaseBtnViewTag + 3){ mineString = @"10"; maxString = @"50"; } else if (btnTag == kBaseBtnViewTag + 4){ mineString = @"50"; maxString = @"100"; } else if (btnTag == kBaseBtnViewTag + 5){ mineString = @"100"; } KDPScreenBtnView *mineView = [self.view viewWithTag:kBaseBtnViewTag]; KDPScreenBtnView *maxView = [self.view viewWithTag:kBaseBtnViewTag+1]; mineView.text = mineString; maxView.text = maxString; } - (void)setUpProfitTextWithTag:(NSInteger )btnTag{ NSString *profitString = @""; if (btnTag == kBaseBtnViewTag + 2) { profitString = @"20"; } else if (btnTag == kBaseBtnViewTag + 3){ profitString = @"30"; } else if (btnTag == kBaseBtnViewTag + 4){ profitString = @"40"; } else if (btnTag == kBaseBtnViewTag + 5){ profitString = @"50"; } KDPScreenBtnView *proFileView = [self.view viewWithTag:kBaseBtnViewTag]; proFileView.text = profitString; } - (void)resetAction:(UIButton *)sender{ [self resetBtnView]; KDPScreenBtnView *mineView = [self.view viewWithTag:kBaseBtnViewTag]; KDPScreenBtnView *maxView = [self.view viewWithTag:kBaseBtnViewTag+1]; mineView.text = @""; maxView.text = @""; [self dismissViewControllerAnimated:YES completion:nil]; if (self.resetBlock) { self.resetBlock(_isPrice); } } - (void)confirmAction:(UIButton *)sender{ KDPScreenBtnView *mineView = [self.view viewWithTag:kBaseBtnViewTag]; KDPScreenBtnView *maxView = [self.view viewWithTag:kBaseBtnViewTag+1]; [self dismissViewControllerAnimated:YES completion:nil]; if (_isPrice) { self.priceBlock(mineView.text, maxView.text); } else{ self.profitBlock(mineView.text); } } - (void)textFieldValueChanged:(UITextField *)textfield{ KDPScreenBtnView *btnView = (KDPScreenBtnView *)textfield.superview; KDPScreenBtnView *minView = [self.view viewWithTag:kBaseBtnViewTag]; KDPScreenBtnView *maxView = [self.view viewWithTag:kBaseBtnViewTag + 1]; if (_isPrice == NO) { [self setBtnColorWithProfitText:minView.text]; if (btnView.tag == kBaseBtnViewTag) { if ([textfield.text integerValue] >= 100) { [MBProgressHUD showError:@"利润不可超过100%"]; textfield.text = [textfield.text substringToIndex:textfield.text.length-1]; } } } else{ [self setBtnColorWithMinText:minView.text maxText:maxView.text]; } } - (void)setParams:(NSDictionary *)params{ _params = params; NSString *minText = [params valueForKey:@"start_price"]; NSString *maxText = [params valueForKey:@"end_price"]; NSString *profitText = [params valueForKey:@"start_tk_rate"]; KDPScreenBtnView *mineBtnView = [self.view viewWithTag:kBaseBtnViewTag]; KDPScreenBtnView *maxBtnView = [self.view viewWithTag:kBaseBtnViewTag+1]; if (_isPrice == YES) { mineBtnView.text = minText; maxBtnView.text = maxText; [self setBtnColorWithMinText:minText maxText:maxText]; } else{ mineBtnView.text = profitText; [self setBtnColorWithProfitText:profitText]; } } - (void)setBtnColorWithMinText:(NSString *)minText maxText:(NSString *)maxText{ KDPScreenBtnView *tenBtnView = [self.view viewWithTag:kBaseBtnViewTag + 2]; KDPScreenBtnView *fiveBtnView = [self.view viewWithTag:kBaseBtnViewTag + 3]; KDPScreenBtnView *thirdBtnView = [self.view viewWithTag:kBaseBtnViewTag + 4]; KDPScreenBtnView *fourBtnView = [self.view viewWithTag:kBaseBtnViewTag + 5]; self.shouldClear = NO; if ([minText integerValue] == 0 && [maxText integerValue] == 10) { tenBtnView.textColor = [UIColor baseColor]; } else if ([minText integerValue] == 10 && [maxText integerValue] == 50){ fiveBtnView.textColor = [UIColor baseColor]; } else if ([minText integerValue] == 50 && [maxText integerValue] == 100){ thirdBtnView.textColor = [UIColor baseColor]; } else if ([minText integerValue] == 100 && [maxText integerValue] == 0){ fourBtnView.textColor = [UIColor baseColor]; } else{ self.shouldClear = YES; [self resetBtnView]; } } - (void)setBtnColorWithProfitText:(NSString *)profit{ KDPScreenBtnView *firstBtnView = [self.view viewWithTag:kBaseBtnViewTag + 2]; KDPScreenBtnView *twoBtnView = [self.view viewWithTag:kBaseBtnViewTag + 3]; KDPScreenBtnView *thirdBtnView = [self.view viewWithTag:kBaseBtnViewTag + 4]; KDPScreenBtnView *fourBtnView = [self.view viewWithTag:kBaseBtnViewTag + 5]; self.shouldClear = NO; if ([profit integerValue] == 20) { firstBtnView.textColor = [UIColor baseColor]; } else if ([profit integerValue] == 30){ twoBtnView.textColor = [UIColor baseColor]; } else if ([profit integerValue] == 40){ thirdBtnView.textColor = [UIColor baseColor]; } else if ([profit integerValue] == 50){ fourBtnView.textColor = [UIColor baseColor]; } else{ self.shouldClear = YES; [self resetBtnView]; } } - (void)textFieldDidEndEditing:(UITextField *)textField{ KDPScreenBtnView *mineBtnView = [self.view viewWithTag:kBaseBtnViewTag]; KDPScreenBtnView *maxBtnView = [self.view viewWithTag:kBaseBtnViewTag+1]; if (_isPrice == YES) { // 价格 if ([mineBtnView.text integerValue] > 0 && [maxBtnView.text integerValue] > 0 && [maxBtnView.text integerValue] < [mineBtnView.text integerValue]) { [MBProgressHUD showError:@"请输入正确的价格"]; textField.text = @""; } } } - (void)textFieldDidBeginEditing:(UITextField *)textfield{ if (self.shouldClear == YES) { [self resetBtnView]; } } @end