// // IncreaseBalanceController.m // model // // Created by JuYi on 2018/7/16. // Copyright © 2018年 Mine. All rights reserved. // 账户充值 #import "IncreaseBalanceController.h" #import "PayTypeView.h" @interface IncreaseBalanceController () @property (nonatomic, strong) UIScrollView *bgScrollView; @property (nonatomic, strong) UILabel *currentBalanceLabel; //当前余额 @property (nonatomic, strong) UITextField *increaseAmountTF; //充值金额 @property (nonatomic, strong) UITextField *writeTF; @property (nonatomic, strong) UIButton *selectedAmountBtn; //选中充值金额按钮 @property (nonatomic, strong) UIButton *selectedPayTypeBtn; //选中支付方式按钮 @property (nonatomic, strong) UIButton *writeBtn;; //选中支付方式按钮 @property (nonatomic, copy) NSString *moneyStr; //选中金额 @property (nonatomic, assign) NSInteger tap; @end @implementation IncreaseBalanceController - (void)viewDidLoad { [super viewDidLoad]; self.tap = 0; // Do any additional setup after loading the view. self.view.backgroundColor = RGBValueColor(0xf7f7f7, 1.0); UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(keyboardHide:)]; //设置成NO表示当前控件响应后会传播到其他控件上,默认为YES。 tapGestureRecognizer.cancelsTouchesInView = NO; //将触摸事件添加到当前view [self.view addGestureRecognizer:tapGestureRecognizer]; //设置导航条 [self creatNavBar]; //设置子试图 [self creatSubViews]; } #pragma mark -- 收起键盘 -(void)keyboardHide:(UITapGestureRecognizer*)tap{ [self.view endEditing:YES]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (void)creatNavBar { self.title = @"账户充值"; // UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom]; btn.frame = CGRectMake(0, 0, 40, 40); [btn setImage:[UIImage imageNamed:@"fanhui2"] forState:UIControlStateNormal]; btn.imageEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0); [btn addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside]; UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:btn]; UIBarButtonItem *nagetiveSpacer = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil]; // nagetiveSpacer.width = -12;//这个值可以根据自己需要自己调整 self.navigationItem.leftBarButtonItems = @[nagetiveSpacer, leftItem]; } - (void)backClick { [self.navigationController popViewControllerAnimated:YES]; } - (void)creatSubViews { // UIScrollView *scrollView = [[UIScrollView alloc] init]; if (@available(iOS 11.0, *) ) { scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever; }else{ self.automaticallyAdjustsScrollViewInsets = NO; } scrollView.frame = self.view.bounds; scrollView.showsVerticalScrollIndicator = NO; scrollView.backgroundColor = RGBValueColor(0xf7f7f7, 1.0); CGFloat height = ScreenHeight <= 667 ? 697 : ScreenHeight; scrollView.contentSize = CGSizeMake(ScreenWidth, height); [self.view addSubview:scrollView]; self.bgScrollView = scrollView; // UILabel *balanceTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 18, 110, 15)]; balanceTitleLabel.text = @"您当前的余额:"; balanceTitleLabel.font = [UIFont systemFontOfSize:15]; balanceTitleLabel.textColor = RGBValueColor(0x333333, 1.0); [scrollView addSubview:balanceTitleLabel]; UILabel *balanceLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(balanceTitleLabel.frame), 18, ScreenWidth / 2, 15)]; balanceLabel.text = @"1566元"; balanceLabel.font = [UIFont systemFontOfSize:15]; balanceLabel.textColor = RGBValueColor(0xfe4086, 1.0); [scrollView addSubview:balanceLabel]; self.currentBalanceLabel = balanceLabel; UIView *upLineView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(balanceTitleLabel.frame) + 18, ScreenWidth, 0.5)]; upLineView.backgroundColor = RGBValueColor(0xdfdfdf, 1.0); [scrollView addSubview:upLineView]; // 按钮布局 CGFloat verticalMargin = 15; CGFloat HorizontalMargin = 20; CGFloat widthBtn = (ScreenWidth - 55) / 2; CGFloat heightBtn = 50; for (int i = 0; i < 4; i++) { NSInteger x = i % 2; NSInteger y = i / 2; UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; btn.frame = CGRectMake(HorizontalMargin + x * verticalMargin + x * widthBtn, CGRectGetMaxY(upLineView.frame) + 15 + y * verticalMargin + y * heightBtn, widthBtn, heightBtn); btn.tag = 200 + i; [btn setBackgroundImage:[UIImage imageNamed:@"kuang"] forState:UIControlStateNormal]; // [btn setBackgroundImage:[UIImage imageNamed:@"kuang2"] forState:UIControlStateSelected]; [btn setTitleColor:RGBValueColor(0x333333, 1.0) forState:UIControlStateNormal]; [btn setTitleColor:RGBValueColor(0xffffff, 1.0) forState:UIControlStateSelected]; btn.titleLabel.font = [UIFont systemFontOfSize:15.f]; [btn addTarget:self action:@selector(selectedAmountBtnAction:) forControlEvents:UIControlEventTouchUpInside]; if (i == 0) { [btn setTitle:@"200元" forState:UIControlStateNormal]; } if (i == 1) { [btn setTitle:@"500元" forState:UIControlStateNormal]; } if (i == 2) { [btn setTitle:@"1000元" forState:UIControlStateNormal]; } if (i == 3) { [btn setTitle:@"2000元" forState:UIControlStateNormal]; // self.writeBtn = [UIButton buttonWithType:UIButtonTypeCustom]; self.writeBtn.frame = CGRectMake(HorizontalMargin, CGRectGetMaxY(btn.frame) + 15, ScreenWidth - 40, heightBtn); [self.writeBtn setBackgroundImage:[UIImage imageNamed:@"changkuang"] forState:UIControlStateNormal]; [self.writeBtn setTitleColor:RGBValueColor(0x333333, 1.0) forState:UIControlStateNormal]; self.writeBtn.titleLabel.font = [UIFont systemFontOfSize:15.f]; [self.writeBtn setTitle:@"请输入其他金额(元)" forState:UIControlStateNormal]; // [writeBtn addTarget:self action:@selector(selectedWriteBtnAction:) forControlEvents:UIControlEventTouchUpInside]; [scrollView addSubview:self.writeBtn]; // UITextField *writeTF = [[UITextField alloc] initWithFrame:CGRectMake(HorizontalMargin, CGRectGetMaxY(btn.frame) + 15, ScreenWidth - 40, heightBtn)]; writeTF.textAlignment = NSTextAlignmentCenter; writeTF.font = [UIFont systemFontOfSize:15]; writeTF.textColor = RGBValueColor(0x333333, 1.0); // increaseAmountTF.placeholder = @"请输入其他金额(元)"; // [increaseAmountTF setValue:RGBValueColor(0x333333, 1.0) forKeyPath:@"_placeholderLabel.textColor"]; // [increaseAmountTF setValue:[UIFont boldSystemFontOfSize:15.0] forKeyPath:@"_placeholderLabel.font"]; writeTF.tag = 700; writeTF.keyboardType = UIKeyboardTypeDecimalPad; writeTF.delegate = self; [scrollView addSubview:writeTF]; self.writeTF = writeTF; } [scrollView addSubview:btn]; } // // UILabel *amountTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, CGRectGetMaxY(self.writeBtn.frame) + 18, 80, 15)]; amountTitleLabel.text = @"充值金额:"; amountTitleLabel.font = [UIFont systemFontOfSize:15]; amountTitleLabel.textColor = RGBValueColor(0x333333, 1.0); [scrollView addSubview:amountTitleLabel]; UITextField *increaseAmountTF = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(amountTitleLabel.frame), CGRectGetMaxY(self.writeBtn.frame) + 18, ScreenWidth / 2, 15)]; increaseAmountTF.textAlignment = NSTextAlignmentLeft; increaseAmountTF.font = [UIFont systemFontOfSize:15]; increaseAmountTF.textColor = RGBValueColor(0xfe4086, 1.0); increaseAmountTF.keyboardType = UIKeyboardTypeDecimalPad; increaseAmountTF.userInteractionEnabled = NO; [scrollView addSubview:increaseAmountTF]; self.increaseAmountTF = increaseAmountTF; UIView *downLineView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(amountTitleLabel.frame) + 18, ScreenWidth, 0.5)]; downLineView.backgroundColor = RGBValueColor(0xdfdfdf, 1.0); [scrollView addSubview:downLineView]; // UILabel *payTypeTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, CGRectGetMaxY(downLineView.frame) + 18, ScreenWidth - 40, 15)]; payTypeTitleLabel.text = @"请选择充值方式:"; payTypeTitleLabel.font = [UIFont systemFontOfSize:15]; payTypeTitleLabel.textColor = RGBValueColor(0x333333, 1.0); [scrollView addSubview:payTypeTitleLabel]; UIView *payTypeLineView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(payTypeTitleLabel.frame) + 18, ScreenWidth, 0.5)]; payTypeLineView.backgroundColor = RGBValueColor(0xdfdfdf, 1.0); [scrollView addSubview:payTypeLineView]; //支付方式选择 UIView *whiteView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(payTypeLineView.frame), ScreenWidth, 173)]; whiteView.backgroundColor = [UIColor whiteColor]; [scrollView addSubview:whiteView]; PayTypeView *zhifubaoView = [[PayTypeView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 86)]; zhifubaoView.iconImageView.image = [UIImage imageNamed:@"zhifubao"]; zhifubaoView.titleLabel.text = @"支付宝充值"; zhifubaoView.subTitleLabel.text = @"推荐有支付宝账号的用户使用"; zhifubaoView.selectedbtn.tag = 300; [zhifubaoView.selectedbtn addTarget:self action:@selector(selectedbtnAction:) forControlEvents:UIControlEventTouchUpInside]; [whiteView addSubview:zhifubaoView]; UIView *mindleLineView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(zhifubaoView.frame), ScreenWidth, 0.5)]; mindleLineView.backgroundColor = RGBValueColor(0xdfdfdf, 1.0); [whiteView addSubview:mindleLineView]; PayTypeView *weixinView = [[PayTypeView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(mindleLineView.frame), ScreenWidth, 86)]; weixinView.iconImageView.image = [UIImage imageNamed:@"weixin"]; weixinView.titleLabel.text = @"微信充值"; weixinView.subTitleLabel.text = @"推荐安装微信的用户使用"; weixinView.selectedbtn.tag = 301; [weixinView.selectedbtn addTarget:self action:@selector(selectedbtnAction:) forControlEvents:UIControlEventTouchUpInside]; [whiteView addSubview:weixinView]; // UIButton *topUpBtn = [UIButton buttonWithType:UIButtonTypeCustom]; topUpBtn.frame = CGRectMake(20, CGRectGetMaxY(whiteView.frame) + 27, ScreenWidth - 40, 45); [topUpBtn setBackgroundImage:[UIImage imageNamed:@"anniu"] forState:UIControlStateNormal]; [topUpBtn setTitle:@"立即充值" forState:UIControlStateNormal]; [topUpBtn setTitleColor:RGBValueColor(0xffffff, 1.0) forState:UIControlStateNormal]; [topUpBtn addTarget:self action:@selector(topUpBtnAction) forControlEvents:UIControlEventTouchUpInside]; [scrollView addSubview:topUpBtn]; } #pragma mark -- 选中的充值金额 - (void)selectedAmountBtnAction:(UIButton *)sender { switch (sender.tag) { case 200: self.increaseAmountTF.text = @"200元"; self.moneyStr = @"200"; break; case 201: self.increaseAmountTF.text = @"500元"; self.moneyStr = @"500"; break; case 202: self.increaseAmountTF.text = @"1000元"; self.moneyStr = @"1000"; break; case 203: self.increaseAmountTF.text = @"2000元"; self.moneyStr = @"2000"; break; default: break; } [self.writeBtn setTitle:@"请输入其他金额(元)" forState:UIControlStateNormal]; self.writeTF.text = @""; self.selectedAmountBtn.selected = NO; sender.selected = !sender.selected; self.selectedAmountBtn = sender; } #pragma mark --  请输入金额 - (void)selectedWriteBtnAction:(UIButton *)sender { self.selectedAmountBtn.selected = NO; self.moneyStr = @""; self.increaseAmountTF.text = @""; } #pragma mark -- 选择支付方式 - (void)selectedbtnAction:(UIButton *)sender { self.selectedPayTypeBtn.selected = NO; sender.selected = !sender.selected; self.selectedPayTypeBtn = sender; } #pragma mark -- 立即充值 - (void)topUpBtnAction { NSLog(@"立即充值"); } #pragma mark ----- UITextFieldDelegate - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField { [self.writeBtn setTitle:@"" forState:UIControlStateNormal]; self.selectedAmountBtn.selected = NO; self.moneyStr = @""; self.increaseAmountTF.text = @""; return YES; } - (void)textFieldDidEndEditing:(UITextField *)textField { if (textField.text.length == 0) { [self.writeBtn setTitle:@"请输入其他金额(元)" forState:UIControlStateNormal]; } else { self.increaseAmountTF.text = [NSString stringWithFormat:@"%@元",textField.text]; } self.moneyStr = textField.text; NSLog(@"self.moneyStr = %@", self.moneyStr); } - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { if (textField.tag == 700) { if(![self isValidAboutInputText:textField shouldChangeCharactersInRange:range replacementString:string decimalNumber:2]) { return NO; } } return YES; } //输入框中只能输入数字和小数点,且小数点只能输入一位,参数number 可以设置小数的位数,该函数在-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string调用; - (BOOL)isValidAboutInputText:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string decimalNumber:(NSInteger)number { NSScanner *scanner = [NSScanner scannerWithString:string]; NSCharacterSet *numbers; NSRange pointRange = [textField.text rangeOfString:@"."]; if ( (pointRange.length > 0) && (pointRange.location < range.location || pointRange.location > range.location + range.length) ){ numbers = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"]; }else{ numbers = [NSCharacterSet characterSetWithCharactersInString:@"0123456789."]; } if ( [textField.text isEqualToString:@""] && [string isEqualToString:@"."] ){ return NO; } short remain = number; //保留 number位小数 NSString *tempStr = [textField.text stringByAppendingString:string]; NSUInteger strlen = [tempStr length]; if(pointRange.length > 0 && pointRange.location > 0){ //判断输入框内是否含有“.”。 if([string isEqualToString:@"."]){ //当输入框内已经含有“.”时,如果再输入“.”则被视为无效。 return NO; } if(strlen > 0 && (strlen - pointRange.location) > remain+1){ //当输入框内已经含有“.”,当字符串长度减去小数点前面的字符串长度大于需要要保留的小数点位数,则视当次输入无效。 return NO; } } NSRange zeroRange = [textField.text rangeOfString:@"0"]; if(zeroRange.length == 1 && zeroRange.location == 0){ //判断输入框第一个字符是否为“0” if(![string isEqualToString:@"0"] && ![string isEqualToString:@"."] && [textField.text length] == 1){ //当输入框只有一个字符并且字符为“0”时,再输入不为“0”或者“.”的字符时,则将此输入替换输入框的这唯一字符。 textField.text = string; return NO; }else{ if(pointRange.length == 0 && pointRange.location > 0){ //当输入框第一个字符为“0”时,并且没有“.”字符时,如果当此输入的字符为“0”,则视当此输入无效。 if([string isEqualToString:@"0"]){ return NO; } } } } NSString *buffer; if ( ![scanner scanCharactersFromSet:numbers intoString:&buffer] && ([string length] != 0) ){ return NO; }else{ return YES; } } @end