1. .h文件
#import <Foundation/Foundation.h>
typedef void(^ValidatePayPasswordType) (NSString *str);//
typedef void(^ForgetPasswordType) (void);//忘记密码
typedef void(^CancelPasswordType) (void);//取消
@interface InputPasswordManager : NSObject
@property(nonatomic,strong)ValidatePayPasswordType ValidatePasswordBlock;
@property(nonatomic,strong)ForgetPasswordType forgetPasswordBlock;
@property(nonatomic,strong)CancelPasswordType CancelPasswordBlock;
+ (instancetype)defaultManager;
- (void)showPasswordInputView;
@end
@interface InputPasswordManager ()
@property(nonatomic)UIView *bgView;
@property(nonatomic)UIView *blackView;
@property(nonatomic)UIView *inputView;
@property(nonatomic)UIView *passView;
@property(nonatomic)NSMutableString *pwbStr;
@end
@implementation InputPasswordManager
+ (instancetype)defaultManager{
static InputPasswordManager *s_manager=nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
s_manager=[[InputPasswordManager alloc]init];
});
return s_manager;
}
- (id)init{
if (self=[super init]) {
_pwbStr=[[NSMutableString alloc]init];
}
return self;
}
- (void)evaluatePolicy
{
LAContext *context = [[LAContext alloc] init];
__block NSString *msg;
[context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:NSLocalizedString(@"UNLOCK_ACCESS_TO_LOCKED_FATURE", nil) reply:
^(BOOL success, NSError *authenticationError) {
if (success) {
msg =[NSString stringWithFormat:NSLocalizedString(@"EVALUATE_POLICY_SUCCESS", nil)];
} else {
msg = [NSString stringWithFormat:NSLocalizedString(@"EVALUATE_POLICY_WITH_ERROR", nil), authenticationError.localizedDescription];
}
}];
}
- (void)showPasswordInputView{
UIWindow * windows = [self getRootWindow];
windows.backgroundColor = [UIColor clearColor];
if (self.bgView==nil) {
self.bgView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, TCScreenW, TCScreenH)];
self.bgView.backgroundColor=[UIColor clearColor];
_blackView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, TCScreenW, TCScreenH)];
_blackView.backgroundColor=[UIColor blackColor];
_blackView.alpha=0.5;
[self.bgView addSubview:_blackView];
_inputView=[[UIView alloc]initWithFrame:CGRectMake(0, TCScreenH, TCScreenW, TCScreenH)];
[self.bgView addSubview:_inputView];
UIImageView *backImageView = [[UIImageView alloc] init];
[_inputView addSubview:backImageView];
backImageView.image = [UIImage imageNamed:@"bai_bg"];
backImageView.clipsToBounds = YES;
backImageView.layer.cornerRadius = 3;
backImageView.contentMode = UIViewContentModeScaleAspectFill;
backImageView.sd_layout
.leftSpaceToView(_inputView,TCAdWidth(38))
.rightSpaceToView(_inputView,TCAdWidth(38))
.topSpaceToView(_inputView,TCAdHeight(179))
.heightIs(TCAdHeight(171));
[backImageView.image applyTintEffectWithColor:[UIColor yellowColor]];
UIView *topView = [[UIView alloc] init];
[_inputView addSubview:topView];
topView.backgroundColor = [UIColor clearColor];
topView.layer.cornerRadius = 3;
topView.clipsToBounds = YES;
topView.sd_layout
.leftSpaceToView(_inputView,TCAdWidth(38))
.rightSpaceToView(_inputView,TCAdWidth(38))
.topSpaceToView(_inputView,TCAdHeight(179))
.heightIs(TCAdHeight(171));
UIImageView * topImageView = [[UIImageView alloc]init];
//topImageView.image = [UIImage imageNamed:@"My_Home_Top_Bg"];
topImageView.image = [TCToolBaseClass imageWithColor:TCColorWhiteGround];
topImageView.contentMode = UIViewContentModeScaleAspectFill;
topImageView.clipsToBounds = YES;
[topView addSubview:topImageView];
topImageView.sd_layout
.leftSpaceToView(topView,TCAdWidth(0))
.rightSpaceToView(topView,TCAdWidth(0))
.topSpaceToView(topView,TCAdHeight(0))
.heightIs(TCAdHeight(171));
//添加毛玻璃
UIBlurEffect * blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
UIVisualEffectView * effectview = [[UIVisualEffectView alloc] initWithEffect:blur];
[topImageView addSubview:effectview];
effectview.sd_layout
.leftSpaceToView(topImageView,TCAdWidth(0))
.rightSpaceToView(topImageView,TCAdWidth(0))
.topSpaceToView(topImageView,TCAdHeight(0))
.heightIs(TCAdHeight(171));
// 输入支付密码 label
UILabel *tipLabel = [[UILabel alloc] init];
[topView addSubview:tipLabel];
tipLabel.text = @"输入支付密码";
tipLabel.textColor = TCColorEmphasis;
tipLabel.font = TCFontSizeSys19;
tipLabel.sd_layout
.centerXEqualToView(topView)
.topSpaceToView(topView,TCAdHeight(20))
.widthIs(Tool_GetWidth(tipLabel.text, TCFontSizeSys19))
.heightIs(TCHeight(18));
// 标题右边的叉按钮
UIView *bgImageView = [[UIView alloc] init];
[topView addSubview:bgImageView];
bgImageView.sd_layout
.topSpaceToView(topView,TCHeight(0))
.rightSpaceToView(topView,TCWidth(0))
.widthIs(TCWidth(30))
.heightIs(TCHeight(30));
UIImageView *cancelImageView = [[UIImageView alloc] init];
[bgImageView addSubview:cancelImageView];
cancelImageView.image = [UIImage imageNamed:@"Close_Btn"];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickBlackView:)];
[bgImageView addGestureRecognizer:tap];
CGSize cancelImageSize=[UIImage imageNamed:@"Close_Btn"].size;
cancelImageView.sd_layout
.topSpaceToView(bgImageView,TCHeight(10))
.rightSpaceToView(bgImageView,TCWidth(10))
.widthIs(cancelImageSize.width)
.heightIs(cancelImageSize.height);
UILabel *lineLabel = [[UILabel alloc] init];
[topView addSubview:lineLabel];
lineLabel.backgroundColor = TCColorComponentBorder;
lineLabel.sd_layout
.leftSpaceToView(topView,0)
.rightSpaceToView(topView,0)
.topSpaceToView(topView,TCHeight(55))
.heightIs(TCHeight(1));
//存放密码的view
_passView = [[UIView alloc] init];
[topView addSubview:_passView];
_passView.layer.borderColor = TCColorMainBody.CGColor;
_passView.layer.borderWidth = TCWidth(1);
_passView.sd_layout
.centerXEqualToView(topView)
.topSpaceToView(topView,TCAdHeight(80))
.leftSpaceToView(topView,TCAdWidth(31))
.widthIs(TCAdWidth(240))
.heightIs(TCAdHeight(40));
// 存放密码的子控件
for (int i = 0; i < 6; i++) {
UIButton *passBtn = [[UIButton alloc] init];
// 设置边框颜色
[passBtn setTitleColor:TCColorEmphasis forState:UIControlStateNormal];
passBtn.frame = CGRectMake((i%6)*TCAdWidth(40), 0, TCAdWidth(40), TCAdHeight(40));
passBtn.layer.borderColor = TCColorMainBody.CGColor;
passBtn.layer.borderWidth = 0.5;
[_passView addSubview:passBtn];
}
//忘记密码
UIButton *resetPass = [[UIButton alloc] init];
[topView addSubview:resetPass];
[resetPass setTitle:@"忘记密码了?" forState:UIControlStateNormal];
[resetPass setTitleColor:TCColorMainBody forState:UIControlStateNormal];
resetPass.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
resetPass.titleLabel.font = TCFontSystem(15);
[resetPass addTarget:self action:@selector(clickForgotPwd) forControlEvents:UIControlEventTouchUpInside];
resetPass.sd_layout
.rightEqualToView(_passView)
.topSpaceToView(_passView,TCAdHeight(14))
.widthIs(Tool_GetWidth(resetPass.titleLabel.text, TCFontSizeSys13) + 20)
.heightIs(TCHeight(12));
// 存放数字的view
UIView *numberView = [[UIView alloc] init];
[_inputView addSubview:numberView];
numberView.backgroundColor = TCColorBackGround;
numberView.sd_layout
.leftSpaceToView(_inputView,0)
.rightSpaceToView(_inputView,0)
.bottomSpaceToView(_inputView,0)
.heightIs(TCAdHeight(216));
// 数字键盘
for (int i = 0; i < 12; i++) {
UIButton *numberBtn = [[UIButton alloc] init];
[numberBtn.layer setBorderWidth:0.5];
numberBtn.layer.borderColor = TCColorSeparator.CGColor;
// 列数
int rank = i % 3;
// 行数
int row = i / 3;
CGFloat numberBtnW = TCScreenW / 3;
CGFloat numberBtnH = numberView.height / 4;
CGFloat numberBtnX = rank * numberBtnW;
CGFloat numberBtnY = row * numberBtnH;
numberBtn.frame = CGRectMake(numberBtnX, numberBtnY, numberBtnW, numberBtnH);
[numberBtn setTitleColor:TCColorEmphasis forState:UIControlStateNormal];
numberBtn.titleLabel.font = [UIFont fontWithName:@"PingFang-SC-Regular" size:28];
if (i < 9) {
NSString *num = [NSString stringWithFormat:@"%d",i + 1];
[numberBtn setTitle:num forState:UIControlStateNormal];
[numberBtn addTarget:self action:@selector(clickNumberBtn:) forControlEvents:UIControlEventTouchUpInside];
[numberBtn setBackgroundImage:ToolGetImageWithColor(TCGrayBtnHighlightColor) forState:UIControlStateHighlighted];
}else if (i == 9){
[numberBtn setTitle:@"清空" forState:UIControlStateNormal];
numberBtn.titleLabel.font = TCFontSizeSys17;
[numberBtn addTarget:self action:@selector(clickEmptyBtn) forControlEvents:UIControlEventTouchUpInside];
}else if (i == 10){
[numberBtn setTitle:@"0" forState:UIControlStateNormal];
[numberBtn addTarget:self action:@selector(clickNumberBtn:) forControlEvents:UIControlEventTouchUpInside];
[numberBtn setBackgroundImage:ToolGetImageWithColor(TCGrayBtnHighlightColor) forState:UIControlStateHighlighted];
}else if (i == 11){
[numberBtn addTarget:self action:@selector(clickDelete:) forControlEvents:UIControlEventTouchUpInside];
[numberBtn setTitle:@"删除" forState:UIControlStateNormal];
[numberBtn setTitleColor:TCColorEmphasis forState:UIControlStateNormal];
numberBtn.titleLabel.font = TCFontSizeSys17;
}
[numberView addSubview:numberBtn];
}
UIView *lineView=[[UIView alloc]init];
lineView.backgroundColor=[UIColor whiteColor];
lineView.frame=CGRectMake(0, 0, TCScreenW, 0.8);
[numberView addSubview:lineView];
}
[windows addSubview:self.bgView];
[self doAnimation];
}
- (void)doAnimation{
[UIView animateWithDuration:0.2 animations:^{
_blackView.alpha=0.75;
_inputView.frame=CGRectMake(0, 0, TCScreenW, TCScreenH);
}];
}
// 点击数字键盘
- (void)clickNumberBtn:(UIButton *)btn{
[_pwbStr appendString:btn.currentTitle];
// 添加数字到上面
for (UIButton *passBtn in _passView.subviews){
NSString *pass = passBtn.currentTitle;
if (pass.length == 0) {
[passBtn setTitle:@"●" forState:UIControlStateNormal];
break;
}
}
// 监听最后一个密码
UIButton *lastBtn = _passView.subviews.lastObject;
NSString *lastStr = lastBtn.currentTitle;
if (lastStr.length) {
NSString * pwd = [_pwbStr md5String];
if (self.ValidatePasswordBlock) {
self.ValidatePasswordBlock(pwd);
}
[self hiddenPasswordInputView];
}
}
// 点击删除按钮
- (void)clickDelete:(UIButton *)btn{
if (_pwbStr.length!=0) {
_pwbStr=[NSMutableString stringWithString:[_pwbStr substringToIndex:_pwbStr.length-1]];
}
NSInteger count = self.passView.subviews.count;
for (int i = 0; i < count; i++) {
UIButton *passBtn = self.passView.subviews[i];
NSString *passStr = passBtn.currentTitle;
if (passStr.length == 0) {
// 上一个buton
if (i == 0) {
break;
}
UIButton *preBtn = self.passView.subviews[i-1];
[preBtn setTitle:nil forState:UIControlStateNormal];
break;
}
// 最后一个按钮
UIButton *lastBtn = self.passView.subviews[count -1];
NSString *lastStr = lastBtn.currentTitle;
if (lastStr.length) {
UIButton *preBtn = self.passView.subviews[count - 1];
[preBtn setTitle:nil forState:UIControlStateNormal];
break;
}
}
}
// 点击忘记密码
- (void)clickForgotPwd{
[self hiddenPasswordInputView];
if (self.forgetPasswordBlock) {
self.forgetPasswordBlock();
}
}
// 点击清空按钮
- (void)clickEmptyBtn{
for (UIButton *passBtn in self.passView.subviews){
[passBtn setTitle:nil forState:UIControlStateNormal];
}
[_pwbStr setString:@""];
}
- (void)clickBlackView:(UITapGestureRecognizer *)sender{
[self hiddenPasswordInputView];
if (self.CancelPasswordBlock) {
self.CancelPasswordBlock();
}
}
- (void)hiddenPasswordInputView{
[UIView animateWithDuration:0.2 animations:^{
_blackView.alpha=0.0;
_inputView.frame=CGRectMake(0, TCScreenH, TCScreenW, TCScreenH);
} completion:^(BOOL finished) {
[self.bgView removeFromSuperview];
[self clickEmptyBtn];
}];
}
- (UIWindow *)getRootWindow{
NSArray *windows = [UIApplication sharedApplication].windows;
for(UIWindow *window in [windows reverseObjectEnumerator]) {
if ([window isKindOfClass:[UIWindow class]] &&
CGRectEqualToRect(window.bounds, [UIScreen mainScreen].bounds))
return window;
}
return [UIApplication sharedApplication].keyWindow;
}
@end