iOS動(dòng)態(tài)驗(yàn)證碼實(shí)現(xiàn)代碼
更新時(shí)間:2018年04月20日 16:44:10 作者:ProgramDog_Chris
本文通過(guò)實(shí)例代碼給大家介紹了ios動(dòng)態(tài)驗(yàn)證碼的實(shí)現(xiàn)方法,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧
具體代碼如下所示:
//
// AuthcodeView.h
// BSbracelet
//
// Created by Christopher on 17/5/16.
// Copyright © 2017年 ZTracy. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface AuthcodeView : UIView
@property (strong, nonatomic) NSArray *dataArray;//字符素材數(shù)組
@property (strong, nonatomic) NSMutableString *authCodeStr;//驗(yàn)證碼字符串
@end
//
// AuthcodeView.m
// BSbracelet
//
// Created by Christopher on 17/5/16.
// Copyright © 2017年 ZTracy. All rights reserved.
//
#import "AuthcodeView.h"
#define kRandomColor [UIColor colorWithRed:arc4random() % 256 / 256.0 green:arc4random() % 256 / 256.0 blue:arc4random() % 256 / 256.0 alpha:1.0];
#define kLineCount 6
#define kLineWidth 1.0
#define kCharCount 4
#define kFontSize [UIFont systemFontOfSize:arc4random() % 5 + 15]
@implementation AuthcodeView
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
self.layer.cornerRadius = 5.0f;
self.layer.masksToBounds = YES;
self.backgroundColor = kRandomColor;
[self getAuthcode];//獲得隨機(jī)驗(yàn)證碼
}
return self;
}
#pragma mark 獲得隨機(jī)驗(yàn)證碼
- (void)getAuthcode
{
//字符串素材
_dataArray = [[NSArray alloc] 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 = [[NSMutableString alloc] initWithCapacity:kCharCount];
//隨機(jī)從數(shù)組中選取需要個(gè)數(shù)的字符串,拼接為驗(yàn)證碼字符串
for (int i = 0; i < kCharCount; i++)
{
NSInteger index = arc4random() % (_dataArray.count-1);
NSString *tempStr = [_dataArray objectAtIndex:index];
_authCodeStr = (NSMutableString *)[_authCodeStr stringByAppendingString:tempStr];
}
}
#pragma mark 點(diǎn)擊界面切換驗(yàn)證碼
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self getAuthcode];
//setNeedsDisplay調(diào)用drawRect方法來(lái)實(shí)現(xiàn)view的繪制
[self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect
{
[super drawRect:rect];
//設(shè)置隨機(jī)背景顏色
self.backgroundColor = kRandomColor;
//根據(jù)要顯示的驗(yàn)證碼字符串,根據(jù)長(zhǎng)度,計(jì)算每個(gè)字符串顯示的位置
NSString *text = [NSString stringWithFormat:@"%@",_authCodeStr];
CGSize cSize = [@"A" sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:20]}];
int width = rect.size.width/text.length - cSize.width;
int height = rect.size.height - cSize.height;
CGPoint point;
//依次繪制每一個(gè)字符,可以設(shè)置顯示的每個(gè)字符的字體大小、顏色、樣式等
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 = [text characterAtIndex:i];
NSString *textC = [NSString stringWithFormat:@"%C", c];
[textC drawAtPoint:point withAttributes:@{NSFontAttributeName:kFontSize}];
}
//調(diào)用drawRect:之前,系統(tǒng)會(huì)向棧中壓入一個(gè)CGContextRef,調(diào)用UIGraphicsGetCurrentContext()會(huì)取棧頂?shù)腃GContextRef
CGContextRef context = UIGraphicsGetCurrentContext();
//設(shè)置線條寬度
CGContextSetLineWidth(context, kLineWidth);
//繪制干擾線
for (int i = 0; i < kLineCount; i++)
{
UIColor *color = kRandomColor;
CGContextSetStrokeColorWithColor(context, color.CGColor);//設(shè)置線條填充色
//設(shè)置線的起點(diǎn)
pX = arc4random() % (int)rect.size.width;
pY = arc4random() % (int)rect.size.height;
CGContextMoveToPoint(context, pX, pY);
//設(shè)置線終點(diǎn)
pX = arc4random() % (int)rect.size.width;
pY = arc4random() % (int)rect.size.height;
CGContextAddLineToPoint(context, pX, pY);
//畫線
CGContextStrokePath(context);
}
}
@end
使用
-(void)recordBtnClick:(UIButton *)recordBtn
{
DomodelView = [[UIView alloc]initWithFrame:CGRectMake(0,0,kScreenWidth,kScreenHeight)];
DomodelView.backgroundColor = [UIColor colorWithRed:0.3 green:0.3 blue:0.3 alpha:0.6];
authBgview = [[UIView alloc]initWithFrame:CGRectMake(30, 120, self.view.frame.size.width-60, 220)];
authBgview.backgroundColor = [UIColor whiteColor]; //顯示驗(yàn)證碼界面
authBgview.layer.cornerRadius = 8;
authBgview.layer.masksToBounds = YES;
authCodeView = [[AuthcodeView alloc] initWithFrame:CGRectMake(10, 10, kScreenWidth-80, 45)];
[authBgview addSubview:authCodeView];
//提示文字
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 65, kScreenWidth-80, 40)];
label.text = @"點(diǎn)擊圖片換驗(yàn)證碼";
label.font = [UIFont systemFontOfSize:15];
label.textColor = [UIColor grayColor];
[authBgview addSubview:label];
//添加輸入框
_input = [[UITextField alloc] initWithFrame:CGRectMake(10, 120, kScreenWidth-80, 40)];
_input.layer.borderColor = [UIColor lightGrayColor].CGColor;
_input.layer.borderWidth = 2.0;
_input.layer.cornerRadius = 5.0;
_input.font = [UIFont systemFontOfSize:21];
_input.placeholder = @"請(qǐng)輸入驗(yàn)證碼!";
_input.clearButtonMode = UITextFieldViewModeWhileEditing;
_input.backgroundColor = [UIColor clearColor];
_input.textAlignment = NSTextAlignmentCenter;
_input.returnKeyType = UIReturnKeyDone;
_input.delegate = self;
[authBgview addSubview:_input];
UIButton *closeBtn = [[UIButton alloc] initWithFrame:CGRectMake(10,170, kScreenWidth-80, 40)];
[closeBtn addTarget:self action:@selector(removeview) forControlEvents:UIControlEventTouchUpInside];
closeBtn.backgroundColor = RGBCOLOR(34,151,216);
[closeBtn setTitle: @"關(guān)閉" forState: UIControlStateNormal];
[authBgview addSubview:closeBtn];
[DomodelView addSubview:authBgview];
[self.view addSubview:DomodelView];
// InvoiceRecordViewController *InvoiceRecordVc = [[InvoiceRecordViewController alloc] initWithNibName:@"InvoiceRecordViewController" bundle:nil];
// [self.navigationController pushViewController:InvoiceRecordVc animated:YES];
}
#pragma mark 輸入框代理,點(diǎn)擊return 按鈕
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
//判斷輸入的是否為驗(yàn)證圖片中顯示的驗(yàn)證碼
if([_input.text compare:authCodeView.authCodeStr options:NSCaseInsensitiveSearch |NSNumericSearch] ==NSOrderedSame)
{
//正確彈出警告款提示正確
// UIAlertView *alview = [[UIAlertView alloc] initWithTitle:@"恭喜您 ^o^" message:@"驗(yàn)證成功" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
// [alview show];
[DomodelView removeFromSuperview];
InvoiceRecordViewController *InvoiceRecordVc = [[InvoiceRecordViewController alloc] initWithNibName:@"InvoiceRecordViewController" bundle:nil];
[self.navigationController pushViewController:InvoiceRecordVc animated:YES];
}
else
{
//驗(yàn)證不匹配,驗(yàn)證碼和輸入框抖動(dòng)
CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:@"transform.translation.x"];
anim.repeatCount = 1;
anim.values = @[@-20,@20,@-20];
// [authCodeView.layer addAnimation:anim forKey:nil];
[_input.layer addAnimation:anim forKey:nil];
}
return YES;
}
總結(jié)
以上所述是小編給大家介紹的iOS動(dòng)態(tài)驗(yàn)證碼實(shí)現(xiàn)代碼,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
iOS應(yīng)用開(kāi)發(fā)中圖片的拉伸問(wèn)題解決方案
這篇文章主要介紹了iOS應(yīng)用開(kāi)發(fā)中圖片的拉伸問(wèn)題解決方案,有時(shí)圖片的拉伸只需要拉伸中間部分而不拉伸兩端,這是本文所關(guān)注的問(wèn)題,需要的朋友可以參考下2016-02-02
iOS ScrollView嵌套tableView聯(lián)動(dòng)滾動(dòng)的思路與最佳實(shí)踐
這篇文章主要給大家介紹了關(guān)于ScrollView嵌套tableView聯(lián)動(dòng)滾動(dòng)的思路與最佳實(shí)踐,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)各位iOS開(kāi)發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10
iOS實(shí)現(xiàn)多個(gè)彈框按順序依次彈出效果
這篇文章主要為大家詳細(xì)介紹了iOS實(shí)現(xiàn)多個(gè)彈框按順序依次彈出效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07
iOS逆向工程使用dumpdecrypted工具給App脫殼
這篇文章主要介紹了iOS逆向工程使用dumpdecrypted工具給App脫殼的相關(guān)資料,本文圖文并茂給大家介紹的非常詳細(xì),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-09-09

