????前段時(shí)間項(xiàng)目中要做一個(gè)本地圖片驗(yàn)證碼功能,然后寫了個(gè)demo 【demo在此,歡迎star】,源碼講解如下:
先來(lái)一張效果圖壓壓驚


一、自定義View,封裝驗(yàn)證碼方法
1、在KenCodeView.h里面主要是,聲明一些屬性和方法,屬性有:字符數(shù)組,驗(yàn)證碼字符串和展示驗(yàn)證碼的label,在這里聲明的改變驗(yàn)證碼的方法是為了一會(huì)兒在Controller中調(diào)用的。
#import <UIKit/UIKit.h>
@interface KenCodeView : UIView
@property (nonatomic, retain) NSArray *changeArray;//驗(yàn)證碼需要的字符組成數(shù)組
@property (nonatomic, retain) NSMutableString *changeString;//展示的驗(yàn)證碼字符串
@property (nonatomic, retain) UILabel *codeLabel;//展示驗(yàn)證碼字符串的label
//改變驗(yàn)證碼的方法,controller中調(diào)用
-(void)changeCode;
@end
2、KenCodeView.m中初始化時(shí)調(diào)用的方法,設(shè)置了隨機(jī)的背景顏色,并調(diào)用組成驗(yàn)證碼字符方法。
//初始化時(shí)調(diào)用
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
float red = arc4random() % 100 / 100.0;
float green = arc4random() % 100 / 100.0;
float blue = arc4random() % 100 / 100.0;
UIColor *color = [UIColor colorWithRed:red green:green blue:blue alpha:0.2];
//隨機(jī)背景顏色
self.backgroundColor = color;
//調(diào)用組成字符的方法
[self change];
}
return self;
}
3、這是生成驗(yàn)證碼的方法,當(dāng)中調(diào)用生成驗(yàn)證碼的字符方法,setNeedsDisplay是調(diào)用drawRect方法,所以Controller中只需要調(diào)用這個(gè)方法就可以重新生成驗(yàn)證碼。
//Controller中調(diào)用此方法可更換驗(yàn)證碼
-(void)changeCode{
//調(diào)用組成字符的方法
[self change];
//顯示
[self setNeedsDisplay];
}
4、這是生成驗(yàn)證碼的字符方法,根據(jù)你的需求進(jìn)行素材組合和字符控制,我這兒是4個(gè)字符的例子,用self.changString進(jìn)行接收這個(gè)驗(yàn)證碼字符。
//組成驗(yàn)證碼字符的方法
- (void)change
{
//驗(yàn)證碼字符素材
self.changeArray = [[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];
NSMutableString *getStr = [[NSMutableString alloc] initWithCapacity:5];
self.changeString = [[NSMutableString alloc] initWithCapacity:6];
//隨機(jī)從數(shù)組中選取需要個(gè)數(shù)的字符串(我選的4),拼接為驗(yàn)證碼字符,最終的字符串用self.changeString接收
for(NSInteger i = 0; i < 4; i++)
{
NSInteger index = arc4random() % ([self.changeArray count] - 1);
getStr = [self.changeArray objectAtIndex:index];
//這是隨機(jī)的驗(yàn)證碼字符串
self.changeString = (NSMutableString *)[self.changeString stringByAppendingString:getStr];
}
}
5、好!關(guān)鍵的方法來(lái)了,這個(gè)方法是通過(guò)setNeedsDisplay來(lái)調(diào)用的,是繪制方法.我這里面主要代碼都有注釋,請(qǐng)?jiān)敿?xì)觀看!
//繪制
- (void)drawRect:(CGRect)rect {
[super drawRect:rect];
float red = arc4random() % 100 / 100.0;
float green = arc4random() % 100 / 100.0;
float blue = arc4random() % 100 / 100.0;
UIColor *color = [UIColor colorWithRed:red green:green blue:blue alpha:0.5];
//隨機(jī)背景顏色,因?yàn)橹匦赂鼡Q驗(yàn)證碼也要走這個(gè)方法,所以設(shè)置背景顏色
[self setBackgroundColor:color];
//根據(jù)要顯示的驗(yàn)證碼字符串,根據(jù)長(zhǎng)度,計(jì)算每個(gè)字符串顯示的位置
NSString *text = [NSString stringWithFormat:@"%@",self.changeString];
CGSize cSize = [@"S" sizeWithFont:[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 withFont:[UIFont systemFontOfSize:20]];
}
//調(diào)用drawRect之前,系統(tǒng)會(huì)向棧中壓入一個(gè)CGContextRef,調(diào)用UIGraphicsGetCurrentContext()會(huì)取棧頂?shù)腃GContextRef
CGContextRef context = UIGraphicsGetCurrentContext();
//設(shè)置線條的寬度
CGContextSetLineWidth(context, 1.0);
//繪制干擾線
for(int cout = 0; cout < 10; cout++)
{
red = arc4random() % 100 / 100.0;
green = arc4random() % 100 / 100.0;
blue = arc4random() % 100 / 100.0;
color = [UIColor colorWithRed:red green:green blue:blue alpha:0.2];
//設(shè)置線條填充色
CGContextSetStrokeColorWithColor(context, [color CGColor]);
//設(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);
}
}
二、Controller中調(diào)用代碼
1、在ViewController.m里面首先導(dǎo)入KenCodeView,并在ViewDidLoad中初始化;
//初始化剛剛封裝過(guò)的KenCodeView
_KenCodeView = [[KenCodeView alloc] initWithFrame:CGRectMake(50, 100, 82, 32)];
//創(chuàng)建輕擊手勢(shì)
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)];
[_KenCodeView addGestureRecognizer:tap];
[self.view addSubview:_KenCodeView];
2、實(shí)現(xiàn)輕擊手勢(shì)事件,點(diǎn)擊調(diào)用更換驗(yàn)證碼的方法
//輕擊手勢(shì)事件
- (void)tapClick:(UITapGestureRecognizer*)tap{
//調(diào)用更換驗(yàn)證碼的方法
[_KenCodeView changeCode];
//輸出目前驗(yàn)證碼的字符
NSLog(@"%@",_KenCodeView.changeString);
}