iOS开发验证码

//自定义验证码生成view

//  securityCodeView.m

//  SecurityCodeTest

//

//  Created by xxxxx on 2017/5/10.

//  Copyright © 2017小熊. All rights reserved.

//


#import "securityCodeView.h"


#define xxRandomColor  [UIColor colorWithRed:arc4random() % 256 / 256.0 green:arc4random() %256 / 256.0 blue:arc4random() %256 / 256.0 alpha:1.0]

//干扰线个数

#define xxLineCount 6

//干扰线宽度

#define xxLineWidth 1.0

//字符数量

#define xxCharCount 6

//随机字体大小

#define xxFontSize [UIFont systemFontOfSize:arc4random() % 5 + 15]


@implementation securityCodeView


- (instancetype)initWithFrame:(CGRect)frame

{

    self = [superinitWithFrame:frame];

    if (self) {

        self.layer.cornerRadius =5.0f;

        self.layer.masksToBounds =YES;

        self.layer.borderWidth =1;

        self.backgroundColor = [UIColorwhiteColor];

        [selfgetAuthcode];

    }

    returnself;

}

- (void)getAuthcode

{

    //字符串素材

    _dataArray = [[NSArrayalloc] initWithObjects:@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K",@"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z",@"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",@"i",@"j",@"k",@"l",@"m",@"n",@"o",@"p",@"q",@"r",@"s",@"t",@"u",@"v",@"w",@"x",@"y",@"z",nil];

    _authCodeStr = [[NSMutableStringalloc] initWithCapacity:xxCharCount];

    //从字符数组中随机选出字符拼接形成验证码

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

        NSInteger index =arc4random() % (_dataArray.count-1);

        NSString * tempStr = [_dataArrayobjectAtIndex:index];

        _authCodeStr = (NSMutableString *)[_authCodeStrstringByAppendingString:tempStr];

    }

    


}

//点击

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

{

    [selfgetAuthcode];

    

    [selfsetNeedsDisplay];


}

//重写绘制方法

- (void)drawRect:(CGRect)rect

{

    [superdrawRect:rect];

    //算出一个字符的最大占有空间

    self.backgroundColor = [UIColorwhiteColor];

    NSString * text = [NSStringstringWithFormat:@"%@",_authCodeStr];

    CGSize cSize = [@"A"sizeWithAttributes:@{NSFontAttributeName:[UIFontsystemFontOfSize:20]}];

    int width = rect.size.width/text.length - cSize.width;

    int height = rect.size.height - cSize.height;

    //给出每个字符的位置,大小,颜色

    CGPoint point;

    float pX, pY;

    for (int i =0; i < text.length; i++) {

        pX = arc4random() % width + rect.size.width/text.length * i;

        pY = arc4random() % height;

        point = CGPointMake(pX, pY);

        unichar c = [textcharacterAtIndex:i];

        NSString * textC = [NSStringstringWithFormat:@"%C", c];

        [textC drawAtPoint:pointwithAttributes:@{NSFontAttributeName:xxFontSize,NSForegroundColorAttributeName:xxRandomColor}];

       

    }

    //绘制干扰线

    CGContextRef context =UIGraphicsGetCurrentContext();

    CGContextSetLineWidth(context,xxLineWidth);

    

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

        UIColor * color =xxRandomColor;

        CGContextSetStrokeColorWithColor(context, color.CGColor);

        CGContextBeginPath(context);

        CGContextMoveToPoint(context,arc4random() % (int)self.bounds.size.width,arc4random() % (int)self.bounds.size.height);

        CGContextAddLineToPoint(context,arc4random() % (int)self.bounds.size.width,arc4random() % (int)self.bounds.size.height);


        CGContextStrokePath(context);

    }


}




@end


//controller 页面显示, 判断验证码是否正确

//  ViewController.m

//  SecurityCodeTest

//

//  Created by xxxxx on 2017/5/10.

//  Copyright © 2017小熊. All rights reserved.

//


#import "ViewController.h"

#import "securityCodeView.h"




@interface ViewController ()<UITextFieldDelegate,UIAlertViewDelegate>


@property (nonatomic,strong) UITextField * passwordTextField;

@property (nonatomic,strong) securityCodeView * authCodeView;

@end


@implementation ViewController


- (void)viewDidLoad {

    [superviewDidLoad];


    self.view.backgroundColor = [UIColorwhiteColor];

    

    //显示验证码界面

    _authCodeView = [[securityCodeViewalloc] initWithFrame:CGRectMake(30,100, 100, 40)];

    _authCodeView.center =CGPointMake([UIScreenmainScreen].bounds.size.width/2,100);

    [self.viewaddSubview:_authCodeView];

    

    //提示文字

    UILabel *label = [[UILabelalloc] initWithFrame:CGRectMake(CGRectGetMaxX(_authCodeView.frame)+10,80, self.view.frame.size.width-100,40)];

    label.text =@"点击图片换验证码";

    label.font = [UIFontsystemFontOfSize:12];

    label.textColor = [UIColorgrayColor];

    [self.viewaddSubview:label];

    

    //添加输入框

    _passwordTextField = [[UITextFieldalloc] initWithFrame:CGRectMake(30,220, self.view.frame.size.width-60,40)];

    _passwordTextField.layer.borderColor = [UIColorlightGrayColor].CGColor;

    _passwordTextField.layer.borderWidth = 2.0;

    _passwordTextField.layer.cornerRadius = 5.0;

    _passwordTextField.font = [UIFontsystemFontOfSize:21];

    _passwordTextField.placeholder =@"请输入验证码!";

    _passwordTextField.clearButtonMode =UITextFieldViewModeWhileEditing;

    _passwordTextField.backgroundColor = [UIColorclearColor];

    _passwordTextField.textAlignment =NSTextAlignmentCenter;

    _passwordTextField.returnKeyType =UIReturnKeyDone;

    _passwordTextField.delegate =self;

    [self.viewaddSubview:_passwordTextField];

    

    



}

#pragma mark 输入框代理,点击return按钮

- (BOOL)textFieldShouldReturn:(UITextField *)textField

{

//    //判断输入的是否为验证图片中显示的验证码

    //NSCaseInsensitiveSearch 不区分大小写

    if ([_passwordTextField.textcompare:_authCodeView.authCodeStroptions:NSCaseInsensitiveSearch] ==NSOrderedSame)

    {

        //正确弹出警告款提示正确

        UIAlertView *alview = [[UIAlertViewalloc] initWithTitle:@"恭喜您^o^"message:@"验证成功"delegate:selfcancelButtonTitle:@"OK"otherButtonTitles:nil,nil];

        [alview show];

    }

    else

    {

        //验证不匹配,验证码和输入框抖动

        CAKeyframeAnimation * xanimation = [CAKeyframeAnimationanimationWithKeyPath:@"transform.translation.x"];

        xanimation.repeatCount =2;

        xanimation.values =@[@-5,@10,@-5];

        xanimation.repeatDuration =0.5;

        [_passwordTextField.layeraddAnimation:xanimation forKey:nil];

        CAKeyframeAnimation * yanimation = [CAKeyframeAnimationanimationWithKeyPath:@"transform.translation.y"];

        yanimation.repeatDuration =0.5;

        yanimation.repeatCount =2;

        yanimation.values =@[@-5,@10,@-5];

        [_passwordTextField.layeraddAnimation:yanimation forKey:nil];

    }


    

    returnYES;

}


#pragma mark 警告框中方法

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

{

    //清空输入框内容,收回键盘

    if (buttonIndex==0)

    {

        _passwordTextField.text =@"";

        [_passwordTextFieldresignFirstResponder];

    }

}



- (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}



@end




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值