123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293 |
- //
- // JZNumberKeyBoard.m
- // JIZHANG
- //
- // Created by kuxuan on 2018/1/25.
- // Copyright © 2018年 kuxuan. All rights reserved.
- //
- #import "JZNumberKeyBoard.h"
- @interface JZNumberKeyBoard()
- @property (nonatomic,strong)UIButton *completeButton;
- @property (nonatomic,assign)BOOL complete; //是否允许8位
- @property (nonatomic,assign)BOOL isFirst; //是否是第一次输入
- @end
- @implementation JZNumberKeyBoard
- + (instancetype)sharedBoard{
-
- static JZNumberKeyBoard *keyBoard;
- static dispatch_once_t onceToken;
- dispatch_once(&onceToken, ^{
- keyBoard = [[self alloc]init];
- [keyBoard setupWindow];
- });
- return keyBoard;
- }
- - (void)setupWindow{
-
- self.complete = NO;
- self.isFirst = YES;
-
- self.editString = [NSMutableString stringWithString:@""];
- self.window = [[UIView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT - 200, SCREEN_WIDTH, 200)];
- // self.window.windowLevel = UIWindowLevelNormal + 33;
- // self.window.rootViewController = [[UIViewController alloc]init];
- // [self.window makeKeyAndVisible];
-
- NSArray *array1 = @[@"1",@"2",@"3",@"+",@"4",@"5",@"6",@"-"];
- for (int i = 0; i < 8; i++) {
- UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
- button.frame = CGRectMake(SCREEN_WIDTH/4*(i%4), 50*(i/4), SCREEN_WIDTH/4, 50);
- button.backgroundColor = [UIColor JZColorWithHex:0xffffff];
- button.titleLabel.font = FONT_SYS(20);
- [button setTitle:array1[i] forState:UIControlStateNormal];
- [button setTitleColor:[UIColor JZColorWithHex:0x333333] forState:UIControlStateNormal];
- button.tag = 777 + i;
- [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
- button.layer.borderColor = [UIColor JZColorWithHex:0xd8d8d8].CGColor;
- button.layer.borderWidth = 0.5;
- [self.window addSubview:button];
- }
- NSArray *array2 = @[@"7",@"8",@"9",@".",@"0",@"ic_delete"];
- for (int i = 0; i < 6; i++) {
- UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
- button.frame = CGRectMake(SCREEN_WIDTH/4*(i%3), 50*(i/3) + 100, SCREEN_WIDTH/4, 50);
- button.backgroundColor = [UIColor JZColorWithHex:0xffffff];
- button.titleLabel.font = FONT_SYS(20);
- button.tag = 785 + i;
- [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
- if (i == 5) {
- [button setImage:[UIImage imageNamed:@"ic_delete"] forState:UIControlStateNormal];
- }else{
- [button setTitle:array2[i] forState:UIControlStateNormal];
- [button setTitleColor:[UIColor JZColorWithHex:0x333333] forState:UIControlStateNormal];
- }
-
- button.layer.borderColor = [UIColor JZColorWithHex:0xd8d8d8].CGColor;
- button.layer.borderWidth = 0.5;
- [self.window addSubview:button];
- }
-
- UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
- button.frame = CGRectMake(SCREEN_WIDTH*3/4, 100, SCREEN_WIDTH/4, 100);
- button.backgroundColor = [UIColor JZColorWithHex:0xffffff];
- button.titleLabel.font = FONT_SYS(18);
- button.tag = 791;
- [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
- [button setTitle:@"完成" forState:UIControlStateNormal];
- button.backgroundColor = [UIColor baseColor];
- [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
- button.layer.borderColor = [UIColor JZColorWithHex:0xd8d8d8].CGColor;
- button.layer.borderWidth = 0.5;
- [self.window addSubview:button];
- }
- - (void)buttonAction:(UIButton *)btn{
- if (self.isFirst == YES) {
- self.isFirst = NO;
- if (![btn.currentTitle isEqualToString:@"+"]&&![btn.currentTitle isEqualToString:@"-"]) {
- self.editString = @"";
- }
- }
-
-
- if ([self.editString containsString:@"+"]||[self.editString containsString:@"-"]) {
- UIButton *btn = [self.window viewWithTag:791];
- [btn setTitle:@"=" forState:UIControlStateNormal];
- }
- if ([btn.currentTitle isEqualToString:@"="]) {
- self.complete = YES;
- }
- if (self.complete == NO) {
- if (self.editString.length >= 8) {
- if ([btn.currentTitle isEqualToString:@"+"]||[btn.currentTitle isEqualToString:@"-"]) {
-
- }else if ([self.editString containsString:@"+"]||[self.editString containsString:@"-"]) {
- NSArray *stringArray = [self.editString componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"+-"]];
- for (int i = 1;i < stringArray.count; i ++) {
- if ([stringArray[i] length] >= 8) {
- return;
- }
- }
- }else{
- if (btn.currentTitle.length != 0) {
- return;
- }
-
- }
- }
- }
-
- switch (btn.tag - 777) {
- //1
- case 0:
- self.editString = [self.editString stringByAppendingString:@"1"];
- break;
- //2
- case 1:
- self.editString = [self.editString stringByAppendingString:@"2"];
- break;
- //3
- case 2:
- self.editString = [self.editString stringByAppendingString:@"3"];
- break;
- //+
- case 3:{
- if (self.editString.length) {
- NSString *last = [self.editString substringFromIndex:self.editString.length-1];
- if ([last isEqualToString:@"+"]||[last isEqualToString:@"-"]) {
-
- }else{
- self.editString = [self.editString stringByAppendingString:@"+"];
- }
- }else{
- self.editString = [self.editString stringByAppendingString:@"+"];
- }
-
- }
-
- break;
- //4
- case 4:
- self.editString = [self.editString stringByAppendingString:@"4"];
- break;
- //5
- case 5:
- self.editString = [self.editString stringByAppendingString:@"5"];
- break;
- //6
- case 6:
- self.editString = [self.editString stringByAppendingString:@"6"];
- break;
- //-
- case 7:
-
- {
- if (self.editString.length) {
- NSString *last = [self.editString substringFromIndex:self.editString.length-1];
- if ([last isEqualToString:@"+"]||[last isEqualToString:@"-"]) {
-
- }else{
- self.editString = [self.editString stringByAppendingString:@"-"];
- }
- }else{
- self.editString = [self.editString stringByAppendingString:@"-"];
- }
-
- }
-
- break;
- //7
- case 8:
- self.editString = [self.editString stringByAppendingString:@"7"];
- break;
- //8
- case 9:
- self.editString = [self.editString stringByAppendingString:@"8"];
- break;
- //9
- case 10:
- self.editString = [self.editString stringByAppendingString:@"9"];
- break;
- //.
- case 11:{
- NSString *last;
- if (self.editString.length >= 1) {
- last = [self.editString substringFromIndex:self.editString.length-1];
- }
- if (self.editString.length == 0) {
- self.editString = [self.editString stringByAppendingString:@"0."];
- }else if ([last isEqualToString:@"+"]||[last isEqualToString:@"-"]){
- self.editString = [self.editString stringByAppendingString:@"0."];
- }
- else{
- self.editString = [self.editString stringByAppendingString:@"."];
- }
-
- }
-
- break;
- //0
- case 12:
- self.editString = [self.editString stringByAppendingString:@"0"];
- break;
- //去除
- case 13:
- {
- if (self.editString.length != 0) {
- self.editString = [self.editString substringToIndex:self.editString.length - 1];
- }
-
- }
- break;
- //完成
- case 14:
- {
- [self setCompleteAction:btn];
- }
- break;
-
-
- default:
- break;
- }
- if (btn.tag - 777 != 14) {
- [_delegate numberKeyBoardDidEditing:self text:self.editString];
- }
- if ([self.editString containsString:@"+"]||[self.editString containsString:@"-"]) {
- if (self.editString.length == 1) {
- self.editString = @"";
- }
- }
- self.complete = NO;
- }
- - (void)setCompleteAction:(UIButton *)btn{
- // self.complete = YES;
- if ([btn.currentTitle isEqualToString:@"="]) {
- if (![self.editString containsString:@"+"]&&![self.editString containsString:@"-"]) {
- self.editString = @"";
- }
- NSArray *stringArray = [self.editString componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"+-"]];
- float result = 0;
- for (int i = 0; i < stringArray.count; i ++) {
- NSString *str = stringArray[i];
- if (str.length == 0) {
- [MBProgressHUD showError:@"输入错误"];
- return;
- }else{
- if ([self.editString containsString:@"+"]) {
- result = result + str.floatValue;
- }else{
- if (i == 0) {
- result = result + str.floatValue;
- }else{
- result = result - str.floatValue;
- }
- }
-
- }
- }
- self.editString = [NSString stringWithFormat:@"%.2f",result];
- [_delegate numberKeyBoardDidEditing:self text:self.editString];
- [btn setTitle:@"完成" forState:UIControlStateNormal];
- self.complete = YES;
-
- }else{
- [_delegate numberKeyBoardDidEndEditing:self text:self.editString];
- }
- }
- - (void)clear{
- self.editString = @"";
- self.complete = NO;
- self.isFirst = YES;
- }
- @end
|