iOS 支付密文实现方案

来源:互联网 发布:域名dns劫持检测 编辑:程序博客网 时间:2024/06/11 03:07

#import <UIKit/UIKit.h>

@class WCLPassWordView;


@protocol  WCLPassWordViewDelegate<NSObject>


@optional

/**

 *  监听输入的改变

 */

- (void)passWordDidChange:(WCLPassWordView *)passWord;


/**

 *  监听输入的完成时

 */

- (void)passWordCompleteInput:(WCLPassWordView *)passWord;


/**

 *  监听开始输入

 */

- (void)passWordBeginInput:(WCLPassWordView *)passWord;



@end


IB_DESIGNABLE


@interface WCLPassWordView : UIView<UIKeyInput>


@property (assign,nonatomic) IBInspectableNSUInteger passWordNum;//密码的位数

@property (assign,nonatomic) IBInspectableCGFloat squareWidth;//正方形的大小

@property (assign,nonatomic) IBInspectableCGFloat pointRadius;//黑点的半径

@property (strong,nonatomic) IBInspectableUIColor *pointColor;//黑点的颜色

@property (strong,nonatomic) IBInspectableUIColor *rectColor;//边框的颜色

@property (weak, nonatomic) IBOutletid<WCLPassWordViewDelegate> delegate;

@property (strong,nonatomic, readonly)NSMutableString *textStore;//保存密码的字符串





import "WCLPassWordView.h"


@interface WCLPassWordView ()


@property (strong,nonatomic) NSMutableString *textStore;//保存密码的字符串


@end


@implementation WCLPassWordView


static NSString  *const MONEYNUMBERS = @"0123456789";


- (instancetype)initWithCoder:(NSCoder *)coder {

    self = [superinitWithCoder:coder];

    if (self) {

        self.textStore = [NSMutableStringstring];

        self.squareWidth =45;

        self.passWordNum =6;

        self.pointRadius =6;

        self.rectColor = [UIColorcolorWithRed:51.0/255.0green:51.0/255.0blue:51.0/255.0alpha:1.0];

        self.pointColor = [UIColorcolorWithRed:223.0/255.0green:223.0/255.0blue:223.0/255.0alpha:1.0];

        [selfbecomeFirstResponder];

    }

    return self;

}


/**

 *  设置正方形的边长

 */

- (void)setSquareWidth:(CGFloat)squareWidth {

    _squareWidth = squareWidth;

    [selfsetNeedsDisplay];

}


/**

 *  设置键盘的类型

 */

- (UIKeyboardType)keyboardType {

    returnUIKeyboardTypeNumberPad;

}


/**

 *  设置密码的位数

 */

- (void)setPassWordNum:(NSUInteger)passWordNum {

    _passWordNum = passWordNum;

    [selfsetNeedsDisplay];

}


- (BOOL)becomeFirstResponder {

    if ([self.delegaterespondsToSelector:@selector(passWordBeginInput:)]) {

        [self.delegatepassWordBeginInput:self];

    }

    return [superbecomeFirstResponder];

}


/**

 *  是否能成为第一响应者

 */

- (BOOL)canBecomeFirstResponder {

    return YES;

}


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

    if (![selfisFirstResponder]) {

        [selfbecomeFirstResponder];

    }

}


#pragma mark - UIKeyInput

/**

 *  用于显示的文本对象是否有任何文本

 */

- (BOOL)hasText {

    return self.textStore.length >0;

}


/**

 *  插入文本

 */

- (void)insertText:(NSString *)text {

    if (self.textStore.length < self.passWordNum) {

        //判断是否是数字

        NSCharacterSet *cs = [[NSCharacterSetcharacterSetWithCharactersInString:MONEYNUMBERS]invertedSet];

        NSString*filtered = [[textcomponentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];

        BOOL basicTest = [text isEqualToString:filtered];

        if(basicTest) {

            [self.textStoreappendString:text];

            if ([self.delegaterespondsToSelector:@selector(passWordDidChange:)]) {

                [self.delegatepassWordDidChange:self];

            }

            if (self.textStore.length ==self.passWordNum) {

                if ([self.delegaterespondsToSelector:@selector(passWordCompleteInput:)]) {

                    [self.delegatepassWordCompleteInput:self];

                }

            }

            [self setNeedsDisplay];

        }

    }

}


/**

 *  删除文本

 */

- (void)deleteBackward {

    if (self.textStore.length >0) {

        [self.textStoredeleteCharactersInRange:NSMakeRange(self.textStore.length - 1, 1)];

        if ([self.delegaterespondsToSelector:@selector(passWordDidChange:)]) {

            [self.delegatepassWordDidChange:self];

        }

    }

    [selfsetNeedsDisplay];

}


// Only override drawRect: if you perform custom drawing.

- (void)drawRect:(CGRect)rect {

    CGFloat height = rect.size.height;

    CGFloat width = rect.size.width;

    CGFloat x = (width - self.squareWidth*self.passWordNum)/2.0;

    CGFloat y = (height - self.squareWidth)/2.0;

    CGContextRef context =UIGraphicsGetCurrentContext();

    //画外框

    CGContextAddRect(context,CGRectMake( x, y,self.squareWidth*self.passWordNum,self.squareWidth));

    CGContextSetLineWidth(context,1);

    CGContextSetStrokeColorWithColor(context,self.rectColor.CGColor);

    CGContextSetFillColorWithColor(context, [UIColorwhiteColor].CGColor);

    //画竖条

    for (int i =1; i <= self.passWordNum; i++) {

        CGContextMoveToPoint(context, x+i*self.squareWidth, y);

        CGContextAddLineToPoint(context, x+i*self.squareWidth, y+self.squareWidth);

         CGContextClosePath(context);

    }

    CGContextDrawPath(context,kCGPathFillStroke);

    CGContextSetFillColorWithColor(context,self.pointColor.CGColor);

    //画黑点

    for (int i =1; i <= self.textStore.length; i++) {

        CGContextAddArc(context,  x+i*self.squareWidth -self.squareWidth/2.0, y+self.squareWidth/2,self.pointRadius,0, M_PI*2,YES);

        CGContextDrawPath(context,kCGPathFill);

    }

}



0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 不小心点了赞怎么办 小车间太热了怎么办 在车间上班好热怎么办 英雄杀四星李逵不拉仇恨怎么办 手表玻璃里面有雾水怎么办 dw手表玻璃碎了怎么办 手表玻璃面花了怎么办 有个窝囊的父母怎么办 苹果7p玩游戏卡怎么办 三星玩游戏很卡怎么办 三星打游戏很卡怎么办 香水喷到眼睛里怎么办 萍果平板锁机怎么办? 苹果6开机卡死怎么办 辐射4发夹用完了怎么办 辐射4多的武器怎么办 大姨妈恶心想吐怎么办 玩完游戏想吐怎么办 玩完了海盗船想吐怎么办 戴眼镜恶心想吐怎么办 玩电脑恶心想吐怎么办 玩游戏玩的头疼怎么办 玩游戏头疼想吐怎么办 游戏玩久了头疼怎么办 有3d眩晕症怎么办 玩游戏晕3d怎么办 梦幻西游亏的钱怎么办 普惠卡销户了钱存进去了怎么办 梦幻西游现金变储备了怎么办 孩子挣了钱存不下怎么办 电脑显示副本不是正版怎么办 斗战神师徒一个人删除角色怎么办 起业kx5防盗器不响怎么办 灌浆记录仪存盘满了怎么办 自首后发现无罪证据怎么办 中国劲酒过期了怎么办 玻尿酸流到眼皮怎么办 手指被胶带缠紫了怎么办 打玻尿酸不平整怎么办 孩子被老师体罚我该怎么办 孩子妈妈入狱了我该怎么办