支付密码输入


.h文件

#import <UIKit/UIKit.h>

@protocol payPwsViewDelegate <NSObject>

- (void)payPwsViewDelegate;


@end


@interface PayPwsView : UIView{

}

@property(nonatomic,strong)id <payPwsViewDelegate> delegate;

@end




.m文件

//

//  PayPwsView.m

//  GWhome

//

//  Created by 陈万里 on 15/11/27.

//  Copyright © 2015 chenwanli. All rights reserved.

//


#import "PayPwsView.h"

#define RGB_A(R,G,B,A) [UIColor colorWithRed:R/255.0 green:G/255.0 blue:B/255.0 alpha:A]

// 获取屏幕高度

#define kDeviceHeight [UIScreen mainScreen].bounds.size.height

// 获取屏幕宽度

#define kDeviceWidth  [UIScreen mainScreen].bounds.size.width


#define FONTNAME(font,name) [UIFont fontWithName:name size:font];

@implementation PayPwsView


- (id)initWithFrame:(CGRect)frame{

    self = [super initWithFrame:frame];

    if (self) {

        self.backgroundColor = RGB_A(0, 0, 0, 0.5);

        

        UIView *bgView =[[UIView alloc]initWithFrame:CGRectMake((kDeviceWidth - 280) / 2, kDeviceHeight - 400, 280, 150)];

        bgView.layer.cornerRadius = 5;

        bgView.layer.masksToBounds = YES;

        bgView.backgroundColor = [UIColor whiteColor];

        [self addSubview:bgView];

        

        UILabel *pwsLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, bgView.frame.size.width, 40)];

        pwsLabel.textAlignment = 1;

        pwsLabel.font = FONTNAME(18, @"Helvetica-Bold");

        pwsLabel.text = @"请输入支付密码";

        [bgView addSubview:pwsLabel];

        

        //    分割线

        UIImageView *separatorImg = [[UIImageView alloc]initWithFrame:CGRectMake(0, 39, bgView.frame.size.width, 1)];

        separatorImg.backgroundColor = [UIColor redColor];

        [bgView addSubview:separatorImg];

        

        float initialX = (bgView.frame.size.width - 40 * 6) / 2;

        for (int i = 0; i < 6; i++) {

            UITextField *pwsField = [[UITextField alloc]initWithFrame:CGRectMake(initialX + i * 40, 75, 40, 40)];

            pwsField.textAlignment = 1;

            pwsField.tag = i + 100;

            pwsField.keyboardType = UIKeyboardTypeNumberPad;

            pwsField.layer.borderWidth = 0.5;

            pwsField.layer.borderColor = [UIColor blackColor].CGColor;

            pwsField.secureTextEntry = YES;

            pwsField.userInteractionEnabled = NO;

            [bgView addSubview:pwsField];

        }

        

        UIButton *pwsBut = [UIButton buttonWithType:UIButtonTypeCustom];

        pwsBut.frame = CGRectMake(initialX, 100, 35 * 6, 35);

        [pwsBut addTarget:self action:@selector(pwsBut:) forControlEvents:UIControlEventTouchUpInside];

        [bgView addSubview:pwsBut];

        

        UITextField *bigPwsField = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, 35, 35)];

        bigPwsField.hidden = YES;

        bigPwsField.textAlignment = 1;

        bigPwsField.tag = 1000;

        bigPwsField.keyboardType = UIKeyboardTypeNumberPad;

        [bigPwsField addTarget:self action:@selector(pwsField:) forControlEvents:UIControlEventEditingChanged];

        [bgView addSubview:bigPwsField];

        

    }

    return self;

}


- (void)pwsBut:(UIButton *)sender{

    UITextField *pwsField = (UITextField *)[self viewWithTag:1000];

    if (sender == nil) {

        pwsField.text = @"";

    }

    [self distributionPws];

    [pwsField becomeFirstResponder];

}


- (void)distributionPws{

    UITextField *bigPwsField = (UITextField *)[self viewWithTag:1000];

    for (int i = 0; i < 6; i++) {

        if (i < bigPwsField.text.length) {

            UITextField *pwsField = (UITextField *)[self viewWithTag:100 + i];

            pwsField.text = [bigPwsField.text substringWithRange:NSMakeRange(i,1)];

        }else{

            UITextField *pwsField = (UITextField *)[self viewWithTag:100 + i];

            pwsField.text = @"";

        }

    }

}


- (void)pwsField:(UITextField *)sender{

    if (sender.text.length < 7) {

        [self distributionPws];

        if (sender.text.length == 6) {

            [self kenEnd];

            [self.delegate payPwsViewDelegate];

        }

    }else{

        sender.text = [sender.text substringWithRange:NSMakeRange(0,sender.text.length - 1)];

    }

}


- (void)setFrame:(CGRect)frame{

    [super setFrame:frame];

    if (frame.origin.y < 1) {

        [self pwsBut:nil];

    }

}


- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    [self kenEnd];

}


- (void)kenEnd{

    [self.superview endEditing:YES];

    [UIView animateWithDuration:0.5 animations:^{

        self.frame = CGRectMake(0,kDeviceHeight,kDeviceWidth, kDeviceHeight);

    }];

}

@end


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值