- 1#import "My_KeyBoardView.h"2#define kScreenWidth[UIScreen mainScreen].bounds.size.width 3#define kScreenHeight[UIScreen mainScreen].bounds.size.height 4 5@implementation My_KeyBoardView 6 7 8 - (id) initWithNumber: (NSNumber * ) number;
- 9 {
- 10 self = [super init];
- 11
- if (self) {
- 12 13 self.backgroundColor = [UIColor redColor];
- 14 self.frame = CGRectMake(0, kScreenHeight - 150, kScreenHeight, 150);
- 15 16[self initKeyBoardNumber_1];
- 17
- }
- 18
- return self;
- 19
- }
- 20 - (void) initKeyBoardNumber_1 21 {
- 22 self.frame = CGRectMake(0, kScreenHeight - 243, kScreenWidth, 243);
- 23 int space = 1;
- 24
- for (int i = 0; i < 9; i++) {
- 25 NSString * str = [NSString stringWithFormat: @"%d", i + 1];
- 26 UIButton * button = [UIButton buttonWithType: UIButtonTypeSystem];
- 27
- if (i < 3) {
- 28 button.frame = CGRectMake(i % 3 * (kScreenWidth / 4) + space, i / 3 * 61, kScreenWidth / 4 - 1, 60);
- 29
- }
- 30
- else {
- 31 button.frame = CGRectMake(i % 3 * (kScreenWidth / 4) + space, i / 3 * 60 + i / 3 * space, kScreenWidth / 4 - 1, 60);
- 32
- }
- 33 button.backgroundColor = [UIColor whiteColor];
- 34[button setTitleColor: [UIColor blackColor] forState: UIControlStateNormal];
- 35 button.titleLabel.font = [UIFont systemFontOfSize: 24];
- 36[button setTitle: str forState: UIControlStateNormal];
- 37 button.tag = i + 1;
- 38[button addTarget: self action: @selector(keyBoardAciont: ) forControlEvents: UIControlEventTouchUpInside];
- 39[self addSubview: button];
- 40
- }
- 41 UIButton * dian = [UIButton buttonWithType: UIButtonTypeSystem];
- 42 dian.frame = CGRectMake(space, 60 * 3 + 3, kScreenWidth / 4 - 1, 60);
- 43 dian.backgroundColor = [UIColor whiteColor];
- 44[dian setTitleColor: [UIColor blackColor] forState: UIControlStateNormal];
- 45 dian.titleLabel.font = [UIFont systemFontOfSize: 24];
- 46[dian addTarget: self action: @selector(keyBoardAciont: ) forControlEvents: UIControlEventTouchUpInside];
- 47[dian setTitle: @"."forState: UIControlStateNormal];
- 48 dian.tag = 11;
- 49[self addSubview: dian];
- 50 UIButton * ling = [UIButton buttonWithType: UIButtonTypeSystem];
- 51 ling.frame = CGRectMake(kScreenWidth / 4 + 1 * space, 60 * 3 + 3, kScreenWidth / 4 - 1, 60);
- 52 ling.backgroundColor = [UIColor whiteColor];
- 53[ling setTitleColor: [UIColor blackColor] forState: UIControlStateNormal];
- 54 ling.titleLabel.font = [UIFont systemFontOfSize: 24];
- 55[ling setTitle: @"0"forState: UIControlStateNormal];
- 56 ling.tag = 0;
- 57[ling addTarget: self action: @selector(keyBoardAciont: ) forControlEvents: UIControlEventTouchUpInside];
- 58[self addSubview: ling];
- 59 60 UIButton * computer = [UIButton buttonWithType: UIButtonTypeSystem];
- 61 computer.frame = CGRectMake(kScreenWidth / 4 * 2 + space, 60 * 3 + 3, kScreenWidth / 4 - 1, 60);
- 62 computer.backgroundColor = [UIColor whiteColor];
- 63[computer setTitle: @"系统键盘"forState: UIControlStateNormal];
- 64 computer.tag = 12;
- 65[computer addTarget: self action: @selector(keyBoardAciont: ) forControlEvents: UIControlEventTouchUpInside];
- 66[self addSubview: computer];
- 67 68 UIButton * delete = [UIButton buttonWithType: UIButtonTypeSystem];
- 69 delete.frame = CGRectMake(kScreenWidth / 4 * 3 + space, 1, kScreenWidth / 4 - 1, 122);
- 70[delete addTarget: self action: @selector(keyBoardAciont: ) forControlEvents: UIControlEventTouchUpInside];
- 71 delete.tag = 10;
- 72 73 UIImageView * deleteImage = [[UIImageView alloc] initWithFrame: CGRectMake((kScreenWidth / 4 - 1 - 28) * 1.0 / 2, 50, 28, 20)];
- 74 deleteImage.image = [UIImage imageNamed: @"goumai_03"];
- 75[delete addSubview: deleteImage];
- 76 77 // [delete setBackgroundImage:[UIImage imageNamed:@"goumai_03"] forState:UIControlStateNormal];
- 78[self addSubview: delete];
- 79 80 UIButton * confirm = [UIButton buttonWithType: UIButtonTypeSystem];
- 81 confirm.frame = CGRectMake(kScreenWidth / 4 * 3 + space, 61 * 2, kScreenWidth / 4 - 1, 122);
- 82 confirm.backgroundColor = [UIColor blueColor];
- 83[confirm setTitleColor: [UIColor whiteColor] forState: UIControlStateNormal];
- 84 confirm.titleLabel.font = [UIFont systemFontOfSize: 20];
- 85[confirm setTitle: @"确 定"forState: UIControlStateNormal];
- 86[confirm addTarget: self action: @selector(keyBoardAciont: ) forControlEvents: UIControlEventTouchUpInside];
- 87 confirm.tag = 13;
- 88[self addSubview: confirm];
- 89
- }
- 90 91#pragma键盘点击按钮事件92 - (void) keyBoardAciont: (UIButton * ) sender 93 {
- 94 UIButton * btn = (UIButton * ) sender;
- 95 NSInteger number = btn.tag;
- 96 // no delegate, print log info
- 97
- if (nil == _delegate) {
- 98 NSLog(@"button tag [%ld]", (long) number);
- 99
- return;
- 100
- }
- 101 102
- if (number <= 9 && number >= 0) {
- 103[_delegate numberKeyBoard: number];
- 104
- return;
- 105
- }
- 106 107
- if (10 == number) {
- 108[_delegate cancelKeyBoard];
- 109
- return;
- 110
- }
- 111
- if (11 == number) {
- 112[_delegate periodKeyBoard];
- 113
- return;
- 114
- }
- 115
- if (12 == number) {
- 116[_delegate changeKeyBoard];
- 117
- return;
- 118
- }
- 119 120
- if (13 == number) {
- 121[_delegate finishKeyBoard];
- 122
- return;
- 123
- }
- 124 125
- }
- 126@end
来源: