????前段時間項目中要做一個本地圖片驗證碼功能,然后寫了個demo 【demo在此,歡迎star】致讥,源碼講解如下:
先來一張效果圖壓壓驚
界面效果.png
輸出的驗證碼.png
一、自定義View,封裝驗證碼方法
1仇味、在KenCodeView.h里面主要是,聲明一些屬性和方法,屬性有:字符數(shù)組,驗證碼字符串和展示驗證碼的label,在這里聲明的改變驗證碼的方法是為了一會兒在Controller中調用的夷家。
#import <UIKit/UIKit.h>
@interface KenCodeView : UIView
@property (nonatomic, retain) NSArray *changeArray;//驗證碼需要的字符組成數(shù)組
@property (nonatomic, retain) NSMutableString *changeString;//展示的驗證碼字符串
@property (nonatomic, retain) UILabel *codeLabel;//展示驗證碼字符串的label
//改變驗證碼的方法,controller中調用
-(void)changeCode;
@end
2抽高、KenCodeView.m中初始化時調用的方法,設置了隨機的背景顏色,并調用組成驗證碼字符方法。
//初始化時調用
- (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];
//隨機背景顏色
self.backgroundColor = color;
//調用組成字符的方法
[self change];
}
return self;
}
3、這是生成驗證碼的方法,當中調用生成驗證碼的字符方法,setNeedsDisplay是調用drawRect方法,所以Controller中只需要調用這個方法就可以重新生成驗證碼鞭呕。
//Controller中調用此方法可更換驗證碼
-(void)changeCode{
//調用組成字符的方法
[self change];
//顯示
[self setNeedsDisplay];
}
4、這是生成驗證碼的字符方法,根據(jù)你的需求進行素材組合和字符控制,我這兒是4個字符的例子,用self.changString進行接收這個驗證碼字符宛官。
//組成驗證碼字符的方法
- (void)change
{
//驗證碼字符素材
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];
//隨機從數(shù)組中選取需要個數(shù)的字符串(我選的4)葫松,拼接為驗證碼字符,最終的字符串用self.changeString接收
for(NSInteger i = 0; i < 4; i++)
{
NSInteger index = arc4random() % ([self.changeArray count] - 1);
getStr = [self.changeArray objectAtIndex:index];
//這是隨機的驗證碼字符串
self.changeString = (NSMutableString *)[self.changeString stringByAppendingString:getStr];
}
}
5底洗、好!關鍵的方法來了,這個方法是通過setNeedsDisplay來調用的,是繪制方法.我這里面主要代碼都有注釋,請詳細觀看!
//繪制
- (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];
//隨機背景顏色腋么,因為重新更換驗證碼也要走這個方法,所以設置背景顏色
[self setBackgroundColor:color];
//根據(jù)要顯示的驗證碼字符串亥揖,根據(jù)長度珊擂,計算每個字符串顯示的位置
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;
//依次繪制每一個字符,可以設置顯示的每個字符的字體大小、顏色未玻、樣式等
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]];
}
//調用drawRect之前灾而,系統(tǒng)會向棧中壓入一個CGContextRef,調用UIGraphicsGetCurrentContext()會取棧頂?shù)腃GContextRef
CGContextRef context = UIGraphicsGetCurrentContext();
//設置線條的寬度
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];
//設置線條填充色
CGContextSetStrokeColorWithColor(context, [color CGColor]);
//設置線的起點
pX = arc4random() % (int)rect.size.width;
pY = arc4random() % (int)rect.size.height;
CGContextMoveToPoint(context, pX, pY);
//設置線的終點
pX = arc4random() % (int)rect.size.width;
pY = arc4random() % (int)rect.size.height;
CGContextAddLineToPoint(context, pX, pY);
//畫線
CGContextStrokePath(context);
}
}
二扳剿、Controller中調用代碼
1旁趟、在ViewController.m里面首先導入KenCodeView,并在ViewDidLoad中初始化庇绽;
//初始化剛剛封裝過的KenCodeView
_KenCodeView = [[KenCodeView alloc] initWithFrame:CGRectMake(50, 100, 82, 32)];
//創(chuàng)建輕擊手勢
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)];
[_KenCodeView addGestureRecognizer:tap];
[self.view addSubview:_KenCodeView];
2锡搜、實現(xiàn)輕擊手勢事件,點擊調用更換驗證碼的方法
//輕擊手勢事件
- (void)tapClick:(UITapGestureRecognizer*)tap{
//調用更換驗證碼的方法
[_KenCodeView changeCode];
//輸出目前驗證碼的字符
NSLog(@"%@",_KenCodeView.changeString);
}