支付密码页面

来源:互联网 发布:龙神的淘宝店 编辑:程序博客网 时间:2024/05/01 12:19

先看下效果图:

我确定这是你们想要的,我在网上找了好久,都是什么请下载我的github的demo,我不想导入他们的包,和那么多文件,就索性自己写了一个,放心我这个很简单的,你能看懂的。
我先说下思路,第一种方法,让UI给你做6个没有点的框框图片,再给你做6个有点的框框的图片,上面加上透明的uitextfield已获取键盘弹出。用输入的数字数判断你的6个框框的图片要显示有点的还是没点的,虽然笨点,但是技术很简单。第二种是框和点都是用代码画出来的,CoreGraphics.framework系统框架,网上说有点耗性能,我看没什么。。。有说下第二种:新建一个View,继承你的baseView或系统的UIView,我命名为passwordView。因为要重新passwordView的-(void)drawRect:(CGRect)rect方法,所以得新建View。
1.在passwordView.h文件中设置代理:

@protocol  passwordViewDelegate<NSObject>

并添加代理方法:

/** *  监听输入的改变 */- (void)passWordDidChange:(passwordView *)passWord;/** *  监听输入的完成时 */- (void)passWordCompleteInput:(passwordView *)passWord;

并设置属性:

//保存密码的字符串@property (strong, nonatomic) NSString *textStore;//设置代理@property (weak, nonatomic) id<passwordViewDelegate> delegate;

在passwordView.m文件中
1.用UITextField覆盖整个View为了获取键盘和输入的密码位数,继承UITextFieldDelegate。
2.设置UITextField属性:

//输入密码控件@property (nonatomic,strong)UITextField *textFld;

3.重新init方法把textFld添加到passView中:

-(instancetype)init{    if (self = [super init]) {        self.backgroundColor = [UIColor whiteColor];        self.textFld = [[UITextField alloc]init];        _textFld.backgroundColor = [UIColor clearColor];        _textFld.keyboardType = UIKeyboardTypeNumberPad;        _textFld.tintColor = [UIColor clearColor];        _textFld.textColor = [UIColor clearColor];        _textFld.delegate = self;        [self addSubview:_textFld];        [_textFld mas_makeConstraints:^(MASConstraintMaker *make) {            make.edges.mas_equalTo(0);        }];    }    return self;}

4.在UITextFieldDelegate的- (BOOL)textField:(UITextField )textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString )string;方法中获取当前输入的密码位数:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{//因为是UITextField控件,常按会出现“粘贴”的情况,如下是判断粘贴的内容是否为数字,如果是可以粘贴,如果不是粘贴不了。      //判断是否是数字    NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:MONEYNUMBERS] invertedSet];    NSString*filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];    BOOL basicTest = [string isEqualToString:filtered];    if (basicTest) {        self.textStore = [textField.text stringByReplacingCharactersInRange:range withString:string];        if (self.textStore.length < 6) {            if ([self.delegate respondsToSelector:@selector(passWordDidChange:)]) {                [self.delegate passWordDidChange:self];            }            [self setNeedsDisplay];        }        if (self.textStore.length == 6) {            if ([self.delegate respondsToSelector:@selector(passWordCompleteInput:)]) {                [self.delegate passWordCompleteInput:self];            }            [self setNeedsDisplay];        }        return YES;    }else{        return NO;    }}

5.重写-(void)drawRect:(CGRect)rect方法,完成绘图:

-(void)drawRect:(CGRect)rect{    //画外框    UIBezierPath *path1 = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];    [HEXCOLOR(0xcccccc) setStroke];    path1.lineWidth = 1;    [path1 stroke];    //画竖线    UIBezierPath *path2 = [UIBezierPath bezierPath];    path2.lineWidth = 1;    [HEXCOLOR(0xeeeeee) setStroke];    for (int i = 1; i <= 5; i++) {        [path2 moveToPoint:CGPointMake(self.frame.size.width/6*i, 0)];        [path2 addLineToPoint:CGPointMake(self.frame.size.width/6*i, self.frame.size.height)];        [path2 stroke];    }    //画点    for (int i = 1; i <= self.textStore.length; i++) {        UIBezierPath *path = [UIBezierPath bezierPath];        [path addArcWithCenter:CGPointMake(self.frame.size.width/12+self.frame.size.width/6*(i-1), self.center.y) radius:5 startAngle:0 endAngle:M_PI_2*4 clockwise:YES];        [[UIColor blackColor] setStroke];        [[UIColor blackColor] setFill];        path.lineWidth = 1;        [path stroke];        [path fill];    }}

在其它页面中引用passwordView。设置好大小,遵守协议,在.m文件中写好代理方法:

#pragma mark - passwordViewDelegate/** *  监听输入的改变 */- (void)passWordDidChange:(passwordView *)passWord{    NSLog(@"输入改变:%@",passWord.textStore);}/** *  监听输入的完成时 */- (void)passWordCompleteInput:(passwordView *)passWord{    NSLog(@"输入完成:%@",passWord.textStore);}

这样就可以了,可能不太严谨,但可以满足需求。不明白的可以问我,要demo的私信,对了,我参考了http://blog.csdn.net/wang631106979/article/details/51566752的博客,勿喷!

0 0
原创粉丝点击