开发工具:
文件大小: 6kb
下载次数: 0
上传时间: 2015-06-15
详细说明: // // BGKeyBoard.h // BGKeyBoard // // Created by SH-zhangketao on 15/6/11. // Copyright (c) 2015年 SH-zhangketao. All rights reserved. // #import @class BGKeyBoard; @protocol BGKeyBoardDelegate @required - (void)finished; @optional - (void)calculatorInputView:(BGKeyBoard *)inputView didTapKey:(NSString *)key; - (void)calculatorInputViewDidTapBackspace:(BGKeyBoard *)calculatorInputView; @end @interface BGKeyBoard : UIView @property (weak, nonatomi c) id delegate; /**----------------------------------------------------------------------------- * @name Customizing colors * ----------------------------------------------------------------------------- */ @property (strong, nonatomic) UIColor *buttonTitleColor; @property (strong, nonatomic) UIFont *buttonTitleFont; @property (strong, nonatomic) UIColor *buttonHighlightedColor; @property (strong, nonatomic) UIColor *numberButtonBackgroundColor; @property (strong, nonatomic) UIColor *numberButtonBorderColor; @end // // BGKeyBoard.m // BGKeyBoard // // Created by SH-zhangketao on 15/6/11. // Copyright (c) 2015年 SH-zhangketao. All rights reserved. // #import "BGKeyBoard.h" #define kBtnw 65 //按键宽 #define kBtnh 45 //按键高 #define kTotalloc 4 //键盘列数 #define kScreenSize [UIScreen mainScreen].applicationFrame.size //界面尺寸 #define kMarginx (kScreenSize.width - kTotalloc * kBtnw) / (kTotalloc + 1) //横向间隔 #define kMarginy (kScreenSize.width - kTotalloc * kBtnw) / (kTotalloc + 1) - 5 //纵向间隔 @interface BGKeyBoard () @property (strong, nonatomic) NSMutableArray *numbers;//数字集合 @property (strong, nonatomic) NSMutableArray *btns; //按键集合 @end @implementation BGKeyBoard - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { self.backgroundColor = [UIColor whiteColor]; _btns = [[NSMutableArray alloc] init]; _numbers = [[NSMutableArray alloc] init]; NSArray *array = @[@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9"]; //随机获取一个数字序列 array = [array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) { return (arc4random() % 3) - 1; }]; [_numbers addObjectsFromArray:array]; [_numbers addObject:@"."]; [_numbers addObject:@"删除"]; [_numbers addObject:@"数字"]; [_numbers addObject:@"字母"]; [_numbers addObject:@"符号"]; [_numbers addObject:@"确定"]; [self createButton]; } return self; } /** 回执键盘 */ - (void)createButton { for (NSInteger index = 0; index < _numbers.count; index++) { UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; [btn setTitle:_numbers[index] forState:UIControlStateNormal]; btn.backgroundColor = [UIColor lightGrayColor]; NSInteger row = index / kTotalloc;//行号 NSInteger loc = index % kTotalloc;//列号 CGFloat btnx = kMarginx + (kMarginx + kBtnw) * loc; CGFloat btny = kMarginy + (kMarginy + kBtnh) * row; btn.frame = CGRectMake(btnx, btny, kBtnw, kBtnh); [self setBtnAppearance:btn]; btn.tag = index; if (index == 11) { [btn addTarget:self action:@selector(userDidTapBackspace:) forControlEvents:UIControlEventTouchUpInside]; } else if (index > 11) { [btn addTarget:self action:@selector(dsfasdf:) forControlEvents:UIControlEventTouchUpInside]; } else { [btn addTarget:self action:@selector(userDidTapKey:) forControlEvents:UIControlEventTouchUpInside]; } if (index == 15) { btn.backgroundColor = [UIColor colorWithRed:0/255.0 green:170/255.0 blue:245/255.0 alpha:1.0]; } btn.showsTouchWhenHighlighted = YES; [self addSubview:btn]; [_btns addObject:btn]; } } /** 按键外观设置 */ - (void)setBtnAppearance:(UIButton *)btn { btn.layer.cornerRadius = 5.0; btn.clipsToBounds = YES; btn.layer.borderWidth = 0.25f; } /** 删除按钮事件 */ - (void)userDidTapBackspace:(UIButton *)sender { [[UIDevice currentDevice] playInputClick]; if ([self.delegate respondsToSelector:@selector(calculatorInputViewDidTapBackspace:)]) { [self.delegate calculatorInputViewDidTapBackspace:self]; } } /** 按键输入事件 */ - (void)userDidTapKey:(UIButton *)sender { [[UIDevice currentDevice] playInputClick]; if ([self.delegate respondsToSelector:@selector(calculatorInputView:didTapKey:)]) { [self.delegate calculatorInputView:self didTapKey:sender.titleLabel.text]; } } /** 输入完成 */ - (void)dsfasdf:(UIButton *)sender { [[UIDevice currentDevice] playInputClick]; if (sender.tag == 15) { if ([self.delegate respondsToSelector:@selector(finished)]) { [self.delegate finished]; } } } #pragma mark - UIInputViewAudioFeedback - (BOOL)enableInputClicksWhenVisible { return YES; } #pragma mark - Helpers - (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size { UIGraphicsBeginImageContextWithOptions(size, NO, 0); CGContextRef context = UIGraphicsGetCurrentContext(); [color set]; CGContextFillRect(context, CGRectMake(0, 0, size.width, size.height)); UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return image; } #pragma mark - Properties /** 按键标题颜色设置 */ - (void)setButtonTitleColor:(UIColor *)buttonTitleColor { _buttonTitleColor = buttonTitleColor; for (UIButton *numberButton in self.btns) { [numberButton setTitleColor:buttonTitleColor forState:UIControlStateNormal]; } } /** 按键标题字体设置 */ - (void)setButtonTitleFont:(UIFont *)buttonTitleFont { _buttonTitleFont = buttonTitleFont; for (UIButton *numberButton in self.btns) { numberButton.titleLabel.font = buttonTitleFont; } } /** 按键高亮颜色设置 */ - (void)setButtonHighlightedColor:(UIColor *)buttonHighlightedColor { _buttonHighlightedColor = buttonHighlightedColor; for (UIButton *numberButton in self.btns) { [numberButton setBackgroundImage:[self imageWithColor:buttonHighlightedColor size:CGSizeMake(50, 50)] forState:UIControlStateHighlighted]; } } /** 按键背景颜色设置 */ - (void)setNumberButtonBackgroundColor:(UIColor *)numberButtonBackgroundColor { _numberButtonBackgroundColor = numberButtonBackgroundColor; for (UIButton *numberButton in self.btns) { numberButton.backgroundColor = numberButtonBackgroundColor; } } /** 按键边界颜色设置 */ - (void)setNumberButtonBorderColor:(UIColor *)numberButtonBorderColor { _numberButtonBorderColor = numberButtonBorderColor; for (UIButton *numberButton in self.btns) { numberButton.layer.borderColor = numberButtonBorderColor.CGColor; } } @end ...展开收缩
(系统自动生成,下载前可以参看下载内容)
下载文件列表
相关说明
- 本站资源为会员上传分享交流与学习,如有侵犯您的权益,请联系我们删除.
- 本站是交换下载平台,提供交流渠道,下载内容来自于网络,除下载问题外,其它问题请自行百度。
- 本站已设置防盗链,请勿用迅雷、QQ旋风等多线程下载软件下载资源,下载后用WinRAR最新版进行解压.
- 如果您发现内容无法下载,请稍后再次尝试;或者到消费记录里找到下载记录反馈给我们.
- 下载后发现下载的内容跟说明不相乎,请到消费记录里找到下载记录反馈给我们,经确认后退回积分.
- 如下载前有疑问,可以通过点击"提供者"的名字,查看对方的联系方式,联系对方咨询.