选择指纹或密码弹出框
/YSTPaySelectView.h
#import <UIKit/UIKit.h>
@interface YSTPaySelectView : UIView
@property (nonatomic, copy) NSString *title, *detail;
@property (nonatomic, assign) CGFloat money;
@property (nonatomic,copy) void (^completeHandle)(int i);
- (void)show;
@end
///YSTPaySelectView.m
#import "YSTPaySelectView.h"
#define TITLE_HEIGHT 46
#define PAYMENT_WIDTH [UIScreen mainScreen].bounds.size.width-80
#define PWD_COUNT 6
#define DOT_WIDTH 10
#define KEYBOARD_HEIGHT 216
#define KEY_VIEW_DISTANCE 100
#define ALERT_HEIGHT 200
@interface YSTPaySelectView()
@property (nonatomic, strong) UIView *paymentAlert;
@property (nonatomic, strong) UIButton *closeBtn, *passwordBtn, *fingerprintBtn;
@property (nonatomic, strong) UILabel *titleLabel, *line, *detailLabel, *moneyLabel;
@end
@implementation YSTPaySelectView
- (instancetype)init {
self = [super init];
if (self) {
self.frame = [UIScreen mainScreen].bounds;
self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.3f];
[self drawView];
}
return self;
}
- (void)drawView {
if (!_paymentAlert) {
_paymentAlert = [[UIView alloc] initWithFrame:CGRectMake(40, ([UIScreen mainScreen].bounds.size.height - ALERT_HEIGHT)/2, PAYMENT_WIDTH, ALERT_HEIGHT)];
_paymentAlert.layer.cornerRadius = 5.0f;
_paymentAlert.layer.masksToBounds = YES;
_paymentAlert.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.95];
[self addSubview:_paymentAlert];
_titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, PAYMENT_WIDTH, TITLE_HEIGHT)];
_titleLabel.textAlignment = NSTextAlignmentCenter;
_titleLabel.textColor = [UIColor blackColor];
_titleLabel.font = [UIFont systemFontOfSize:17];
[_paymentAlert addSubview:_titleLabel];
_closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
_closeBtn.frame = CGRectMake(0, 0, TITLE_HEIGHT, TITLE_HEIGHT);
[_closeBtn setTitle:@"╳" forState:UIControlStateNormal];
[_closeBtn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
[_closeBtn addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
_closeBtn.titleLabel.font = [UIFont systemFontOfSize:15];
[_paymentAlert addSubview:_closeBtn];
_passwordBtn = [UIButton buttonWithType:UIButtonTypeCustom];
_passwordBtn.frame = CGRectMake(PAYMENT_WIDTH - TITLE_HEIGHT * 2, 0, TITLE_HEIGHT * 2, TITLE_HEIGHT);
[_passwordBtn setTitle:@"使用密码" forState:UIControlStateNormal];
_passwordBtn.titleLabel.font = [UIFont systemFontOfSize:15];
[_passwordBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[_passwordBtn addTarget:self action:@selector(passwordBtnClick) forControlEvents:UIControlEventTouchUpInside];
[_paymentAlert addSubview:_passwordBtn];
_line = [[UILabel alloc] initWithFrame:CGRectMake(0, TITLE_HEIGHT, PAYMENT_WIDTH, 0.5f)];
_line.backgroundColor = [UIColor lightGrayColor];
[_paymentAlert addSubview:_line];
_detailLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, TITLE_HEIGHT + 15, PAYMENT_WIDTH, 20)];
_detailLabel.textAlignment = NSTextAlignmentCenter;
_detailLabel.textColor = [UIColor blackColor];
_detailLabel.font = [UIFont systemFontOfSize:16];
[_paymentAlert addSubview:_detailLabel];
_moneyLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, TITLE_HEIGHT * 2, PAYMENT_WIDTH, 25)];
_moneyLabel.textAlignment = NSTextAlignmentCenter;
_moneyLabel.textColor = [UIColor blackColor];
_moneyLabel.font = [UIFont systemFontOfSize:33];
[_paymentAlert addSubview:_moneyLabel];
_fingerprintBtn = [UIButton buttonWithType:UIButtonTypeCustom];
_fingerprintBtn.frame = CGRectMake(10, TITLE_HEIGHT * 3 + 5, PAYMENT_WIDTH - 20, TITLE_HEIGHT);
[_fingerprintBtn setTitle:@"指纹支付" forState:UIControlStateNormal];
_fingerprintBtn.titleLabel.font = [UIFont systemFontOfSize:17];
_fingerprintBtn.backgroundColor = [UIColor redColor];
[_fingerprintBtn addTarget:self action:@selector(fingerprintBtnClick) forControlEvents:UIControlEventTouchUpInside];
_fingerprintBtn.layer.cornerRadius = 5;
_fingerprintBtn.layer.masksToBounds = YES;
[_paymentAlert addSubview:_fingerprintBtn];
}
}
- (void)passwordBtnClick {
[self dismiss];
if (_completeHandle) {
_completeHandle(1);
}
}
- (void)fingerprintBtnClick {
[self dismiss];
if (_completeHandle) {
_completeHandle(2);
}
}
- (void)show {
UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
[keyWindow addSubview:self];
_paymentAlert.transform = CGAffineTransformMakeScale(1.2f, 1.2f);
_paymentAlert.alpha = 0;
[UIView animateWithDuration:0.7f delay:0.0f usingSpringWithDamping:0.7f initialSpringVelocity:1 options:UIViewAnimationOptionCurveEaseInOut animations:^{
_paymentAlert.transform = CGAffineTransformMakeScale(1.0f, 1.0f);
_paymentAlert.alpha = 1.0;
} completion:nil];
}
- (void)dismiss {
[UIView animateWithDuration:0 animations:^{
_paymentAlert.transform = CGAffineTransformMakeScale(0.5f, 0.5f);
_paymentAlert.alpha = 0;
self.alpha = 0;
} completion:^(BOOL finished) {
[self removeFromSuperview];
}];
}
#pragma mark -
- (void)setTitle:(NSString *)title {
if (_title != title) {
_title = title;
_titleLabel.text = _title;
}
}
- (void)setDetail:(NSString *)detail {
if (_detail != detail) {
_detail = detail;
_detailLabel.text = _detail;
}
}
- (void)setMoney:(CGFloat)money {
if (_money != money) {
_money = money;
_moneyLabel.text = [NSString stringWithFormat:@"¥%.2f元", money];
}
}
@end
输入密码弹出框
///YSTPayView.h
#import <UIKit/UIKit.h>
@interface YSTPayView : UIView
@property (nonatomic, copy) NSString *title, *detail;
@property (nonatomic, assign) CGFloat money;
@property (nonatomic,copy) void (^completeHandle)(NSString *inputPwd);
- (void)show;
@end
YSTPayView.m
#import "YSTPayView.h"
#define TITLE_HEIGHT 46
#define PAYMENT_WIDTH [UIScreen mainScreen].bounds.size.width-80
#define PWD_COUNT 6
#define DOT_WIDTH 10
#define KEYBOARD_HEIGHT 216
#define KEY_VIEW_DISTANCE 100
#define ALERT_HEIGHT 200
@interface YSTPayView()<UITextFieldDelegate> {
NSMutableArray *pwdIndicatorArr;
}
@property (nonatomic, strong) UIView *paymentAlert, *inputView;
@property (nonatomic, strong) UIButton *closeBtn;
@property (nonatomic, strong) UILabel *titleLabel, *line, *detailLabel, *moneyLabel;
@property (nonatomic, strong) UITextField *pwdTextField;
@end
@implementation YSTPayView
- (instancetype)init {
self = [super init];
if (self) {
self.frame = [UIScreen mainScreen].bounds;
self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.3f];
[self drawView];
}
return self;
}
- (void)drawView {
if (!_paymentAlert) {
_paymentAlert = [[UIView alloc] initWithFrame:CGRectMake(40, [UIScreen mainScreen].bounds.size.height - KEYBOARD_HEIGHT - KEY_VIEW_DISTANCE - ALERT_HEIGHT, PAYMENT_WIDTH, ALERT_HEIGHT)];
_paymentAlert.layer.cornerRadius = 5.0f;
_paymentAlert.layer.masksToBounds = YES;
_paymentAlert.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.95];
[self addSubview:_paymentAlert];
_titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, PAYMENT_WIDTH, TITLE_HEIGHT)];
_titleLabel.textAlignment = NSTextAlignmentCenter;
_titleLabel.textColor = [UIColor blackColor];
_titleLabel.font = [UIFont systemFontOfSize:17];
[_paymentAlert addSubview:_titleLabel];
_closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
_closeBtn.frame = CGRectMake(0, 0, TITLE_HEIGHT, TITLE_HEIGHT);
[_closeBtn setTitle:@"╳" forState:UIControlStateNormal];
[_closeBtn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
[_closeBtn addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
_closeBtn.titleLabel.font = [UIFont systemFontOfSize:15];
[_paymentAlert addSubview:_closeBtn];
_line = [[UILabel alloc] initWithFrame:CGRectMake(0, TITLE_HEIGHT, PAYMENT_WIDTH, 0.5f)];
_line.backgroundColor = [UIColor lightGrayColor];
[_paymentAlert addSubview:_line];
_detailLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, TITLE_HEIGHT + 15, PAYMENT_WIDTH, 20)];
_detailLabel.textAlignment = NSTextAlignmentCenter;
_detailLabel.textColor = [UIColor blackColor];
_detailLabel.font = [UIFont systemFontOfSize:16];
[_paymentAlert addSubview:_detailLabel];
_moneyLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, TITLE_HEIGHT * 2, PAYMENT_WIDTH, 25)];
_moneyLabel.textAlignment = NSTextAlignmentCenter;
_moneyLabel.textColor = [UIColor blackColor];
_moneyLabel.font = [UIFont systemFontOfSize:33];
[_paymentAlert addSubview:_moneyLabel];
_inputView = [[UIView alloc] initWithFrame:CGRectMake(15, _paymentAlert.frame.size.height - (PAYMENT_WIDTH -30) / 6 - 15, PAYMENT_WIDTH - 30, (PAYMENT_WIDTH - 30) / 6)];
_inputView.backgroundColor = [UIColor whiteColor];
_inputView.layer.borderColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1.0].CGColor;
_inputView.layer.borderWidth = 1;
[_paymentAlert addSubview:_inputView];
pwdIndicatorArr = [[NSMutableArray alloc]init];
_pwdTextField = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
_pwdTextField.hidden = YES;
_pwdTextField.delegate = self;
_pwdTextField.keyboardType = UIKeyboardTypeNumberPad;
[_inputView addSubview:_pwdTextField];
CGFloat width = _inputView.bounds.size.width / PWD_COUNT;
for (int i = 0; i < PWD_COUNT; i ++) {
UILabel *dot = [[UILabel alloc]initWithFrame:CGRectMake((width - DOT_WIDTH)/2.0f + i * width, (_inputView.bounds.size.height - DOT_WIDTH)/2.0f, DOT_WIDTH, DOT_WIDTH)];
dot.backgroundColor = [UIColor blackColor];
dot.layer.cornerRadius = DOT_WIDTH/2.0;
dot.clipsToBounds = YES;
dot.hidden = YES;
[_inputView addSubview:dot];
[pwdIndicatorArr addObject:dot];
if (i == PWD_COUNT-1) {
continue;
}
UILabel *line = [[UILabel alloc]initWithFrame:CGRectMake((i+1)*width, 0, .5f, _inputView.bounds.size.height)];
line.backgroundColor = [UIColor colorWithRed:.9 green:.9 blue:.9 alpha:1.];
[_inputView addSubview:line];
}
}
}
- (void)show {
UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
[keyWindow addSubview:self];
_paymentAlert.transform = CGAffineTransformMakeScale(0.5f, 0.5f);
_paymentAlert.alpha = 0;
[UIView animateWithDuration:0.7f delay:0.0f usingSpringWithDamping:0.7f initialSpringVelocity:1 options:UIViewAnimationOptionCurveEaseInOut animations:^{
[_pwdTextField becomeFirstResponder];
_paymentAlert.transform = CGAffineTransformMakeScale(1.0f, 1.0f);
_paymentAlert.alpha = 1.0;
} completion:nil];
}
- (void)dismiss {
[_pwdTextField resignFirstResponder];
[UIView animateWithDuration:0 animations:^{
_paymentAlert.transform = CGAffineTransformMakeScale(0.5f, 0.5f);
_paymentAlert.alpha = 0;
self.alpha = 0;
} completion:^(BOOL finished) {
[self removeFromSuperview];
}];
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if (textField.text.length >= PWD_COUNT && string.length) {
//输入的字符个数大于6,则无法继续输入,返回NO表示禁止输入
return NO;
}
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",@"^[0-9]*$"];
if (![predicate evaluateWithObject:string]) {
return NO;
}
NSString *totalString;
if (string.length <= 0) {
totalString = [textField.text substringToIndex:textField.text.length-1];
}
else {
totalString = [NSString stringWithFormat:@"%@%@",textField.text,string];
}
[self setLabWithCount:totalString.length];
if (totalString.length == 6) {
if (_completeHandle) {
[self performSelector:@selector(dismiss) withObject:nil afterDelay:0];
_completeHandle(totalString);
}
}
return YES;
}
- (void)setLabWithCount:(NSInteger)count {
for (UILabel *dot in pwdIndicatorArr) {
dot.hidden = YES;
}
for (int i = 0; i < count; i++) {
((UILabel*)[pwdIndicatorArr objectAtIndex:i]).hidden = NO;
}
}
#pragma mark -
- (void)setTitle:(NSString *)title {
if (_title != title) {
_title = title;
_titleLabel.text = _title;
}
}
- (void)setDetail:(NSString *)detail {
if (_detail != detail) {
_detail = detail;
_detailLabel.text = _detail;
}
}
- (void)setMoney:(CGFloat)money {
if (_money != money) {
_money = money;
_moneyLabel.text = [NSString stringWithFormat:@"¥%.2f元", money];
}
}
@end
// 调用
LAContext *context = [LAContext new];
NSError *error;
context.localizedFallbackTitle = @"";
if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {
YSTPaySelectView *selectView = [[YSTPaySelectView alloc] init];
selectView.title = @"支付";
selectView.detail = @"XXX支付";
selectView.money = 10;
[selectView show];
selectView.completeHandle = ^(int i) {
if (i == 1) {
YSTPayView *payAlert = [[YSTPayView alloc] init];
payAlert.title = @"请输入支付密码";
payAlert.detail = @"支付";
payAlert.money = [[self.userDictionary objectForKey:@"money"] longLongValue]/100.0;
[payAlert show];
payAlert.completeHandle = ^(NSString *inputPassword) {
[self pay:inputPassword];
};
} else if (i == 2) {
[context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:NSLocalizedString(@"请验证已有的指纹,用于支付", nil) reply:^(BOOL success, NSError *error) {
if (success) {
dispatch_async(dispatch_get_main_queue(), ^{
// 此处进行免输入密码支付
});
} else {
}
}];
}
};
} else {
// 此处进行输入密码支付
YSTPayView *payAlert = [[YSTPayView alloc] init];
payAlert.title = @"请输入支付密码";
payAlert.detail = @"支付";
payAlert.money = 10;
[payAlert show];
payAlert.completeHandle = ^(NSString *inputPassword) {
[self pay:inputPassword];
};
}