主要思路是用递归算法创建按钮结合Block实现该功能
首先定义按钮的View
KeyBordView.h
#import <UIKit/UIKit.h>
#define MAINSCREEN [UIScreen mainScreen].bounds
typedef void(^CountClickBlock)(NSString *num);
typedef void(^DeleteClickBlock)(void);
typedef void(^CodeClickBlock)(void);
typedef void(^ScanClickBlock)(void);
@interface KeyBordView : UIView
@property (nonatomic, strong) CountClickBlock countClick;
@property (nonatomic, strong) DeleteClickBlock deleteClick;
@property (nonatomic, strong) CodeClickBlock codeBlock;
@property (nonatomic, strong) ScanClickBlock scanBlock;
@end
KeyBordView.m
@implementation KeyBordView
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
- (instancetype)initWithFrame:(CGRect)frame{
if ([super initWithFrame:frame]) {
[self initView];
}
return self;
}
- (void)initView{
CGFloat W = MAINSCREEN.size.width/4;
for (int i=0; i<=10; i++) {
UIButton *buton = [UIButton buttonWithType:UIButtonTypeCustom];
[buton setBackgroundColor: [UIColor whiteColor]];
[buton setTitle:[NSString stringWithFormat:@"%d",10-i] forState:UIControlStateNormal];
[buton addTarget:self action:@selector(countClick:) forControlEvents:UIControlEventTouchUpInside];
[buton setTitleColor:[UIColor darkGrayColor] forState:0];
if (i==0) {
buton.tag = 100;
buton.frame = CGRectMake(0, 150, W*2, 50);
[buton setTitle:@"0" forState:UIControlStateNormal];
}else if(i == 10){
buton.tag = 110;
buton.frame = CGRectMake(W*2, 150, W, 50);
[buton setTitle:@"." forState:UIControlStateNormal];
}else{
buton.tag = 10-i+100;
buton.frame = CGRectMake(((i-1)%3)*W,(i-1)/3*50 , W, 50);
}
[self addSubview:buton];
}
UIButton *deletBtn = [[UIButton alloc]init];
[deletBtn setTitle:@"删除" forState:0];
[deletBtn setTitleColor:[UIColor darkGrayColor] forState:0];
[deletBtn addTarget:self action:@selector(deleteClick:) forControlEvents:UIControlEventTouchUpInside];
deletBtn.frame = CGRectMake(W*3, 0, W, 50);
[self addSubview:deletBtn];
UIButton *codeMoneyBtn = [[UIButton alloc]init];
[codeMoneyBtn setTitle:@"收款" forState:0];
[codeMoneyBtn setTitleColor:[UIColor darkGrayColor] forState:0];
[codeMoneyBtn addTarget:self action:@selector(codeGetMoney:) forControlEvents:UIControlEventTouchUpInside];
codeMoneyBtn.frame = CGRectMake(W*3, 50, W, 100);
[self addSubview:codeMoneyBtn];
UIButton *scanBtn = [[UIButton alloc]init];
[scanBtn setTitle:@"扫描" forState:0];
[scanBtn setTitleColor:[UIColor darkGrayColor] forState:0];
[scanBtn addTarget:self action:@selector(codeScanClick:) forControlEvents:UIControlEventTouchUpInside];
scanBtn.frame = CGRectMake(W*3, 150, W, 50);
[self addSubview:scanBtn];
}
- (void)countClick:(UIButton *)sender{
self.countClick(sender.titleLabel.text);
}
- (void)deleteClick:(UIButton *)sender{
self.deleteClick();
}
- (void)codeGetMoney:(UIButton *)sender{
NSLog(@"二维码收款");
self.codeBlock();
}
- (void)codeScanClick:(UIButton *)sender{
NSLog(@"扫描");
self.scanBlock();
}
@end
//此处MAINSCREEN是屏幕宽度
最后主控制器调用
#import "KeyBordView.h"
@interface GatheringVC ()
@property (nonatomic, strong) UITextField *tf;
@end
@implementation GatheringVC
- (UITextField *)tf{
if (!_tf) {
UITextField *t = [[UITextField alloc]init];
t.font = [UIFont systemFontOfSize:25];
_tf = t;
}
return _tf;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
// Do any additional setup after loading the view.
KeyBordView *view = [[KeyBordView alloc]init];
view.frame = CGRectMake(0, 120, MAINSCREEN.size.width, 200);
[self.view addSubview:view];
view.countClick = ^(NSString *num) {
if ([num isEqualToString:@"."]) {
if (![_tf.text containsString:@"."]) {
_tf.text = [NSString stringWithFormat:@"%@%@",_tf.text,num];
}
}else{
_tf.text = [NSString stringWithFormat:@"%@%@",_tf.text,num];
}
};
view.deleteClick = ^{
NSString * str = _tf.text;
if (str.length>=1) {
str = [str substringToIndex:([str length]-1)];
_tf.text = str;
}
};
view.scanBlock = ^{
NSLog(@"按钮点击");
};
view.codeBlock = ^{
NSLog(@"按钮点击");
};
[self.view addSubview:self.tf];
_tf.frame = CGRectMake(10, view.center.y+100, MAINSCREEN.size.width-20, 100);
}
真的猛士,敢于直面惨淡的人生,敢于正视淋漓的鲜血。这是怎样的哀痛者和幸福者?然而造化又常常为庸人设计,以时间的流驶,来洗涤旧迹,仅使留下淡红的血色和微漠的悲哀。在这淡红的血色和微漠的悲哀中,又给人暂得偷生,维持着这似人非人的世界。我不知道这样的世界何时是一个尽头!