我們經(jīng)常會(huì)在手機(jī)應(yīng)用中會(huì)看到輸入驗(yàn)證碼,然后操作一個(gè)東西,那么我們應(yīng)該如何去實(shí)現(xiàn)他呢,先看一下效果

verificationCode.gif
1、首先我們定義一個(gè)繼承UIView的類ZYCaptchaView用于、生成驗(yàn)證碼的操作
2、.h文件中我們這樣聲明
/**字符數(shù)組 */
@property (nonatomic , strong)NSArray *characterArray;
/** 驗(yàn)證碼字符串 */
@property (nonatomic , strong)NSMutableString *captchaString;
/** 加載驗(yàn)證碼 */
- (void)loadCaptcha;
3、.m文件中我們具體去實(shí)現(xiàn)
首先,我們?cè)?code>initWithFrame中生成一個(gè)隨機(jī)的驗(yàn)證碼
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
//設(shè)置layer圓角半徑
self.layer.cornerRadius = 5.0;
//影藏邊境
self.layer.masksToBounds = YES;
self.backgroundColor = kRandomColor;
//顯示一個(gè)隨機(jī)驗(yàn)證碼
[self randomCaptcha];
}
return self;
}
#pragma mark -- 更換驗(yàn)證碼
- (void)randomCaptcha {
//數(shù)組中存放的是全部可選的字符,可以是字母,也可以是中文
self.characterArray = [[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:kCharCount];
self.captchaString = [[NSMutableString alloc] initWithCapacity:kCharCount];
//隨機(jī)從數(shù)組中選取需要個(gè)數(shù)的字符,然后拼接為一個(gè)字符串
for (int i = 0 ; i < kCharCount; i ++) {
int index = arc4random() % (self.characterArray.count - 1);
getStr = [self.characterArray objectAtIndex:index];
self.captchaString = [[self.captchaString stringByAppendingString:getStr] copy];
}
//從網(wǎng)絡(luò)獲取字符串,然后把得到的字符串在本地繪制出來(lái)(網(wǎng)絡(luò)獲取步驟在這省略)
}
其次,我們實(shí)現(xiàn)加載驗(yàn)證碼的代碼
/** 加載驗(yàn)證碼 */
- (void)loadCaptcha {
//更換驗(yàn)證碼
[self randomCaptcha];
//重新繪制
[self setNeedsDisplay];
}
最后我們實(shí)現(xiàn)繪制驗(yàn)證碼的代碼,也就是重寫<code>drawRect</code>方法
#pragma mark 繪制界面
- (void)drawRect:(CGRect)rect {
[super drawRect:rect];
//設(shè)置隨機(jī)背景顏色
self.backgroundColor = kRandomColor;
//獲取要顯示的驗(yàn)證碼字符串
NSString *text = [NSString stringWithFormat:@"%@" , self.captchaString];
CGSize cSize = [@"s" 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}];
}
//獲取上下文
CGContextRef context = UIGraphicsGetCurrentContext();
//設(shè)置畫線寬度
CGContextSetLineWidth(context, kLineWidth);
//繪制干擾的彩色直線
for (int i = 0; i < kLineCount; i ++) {
//設(shè)置線的隨機(jī)顏色
UIColor *color = kRandomColor;
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);
}
}
4、在控制器中調(diào)用我們的類
需要注意的是我們需要讓驗(yàn)證碼不區(qū)分大小寫,我們就需要大小寫進(jìn)行轉(zhuǎn)換,你可以將所有字母轉(zhuǎn)換為大寫,也可以轉(zhuǎn)換為小寫。大小寫轉(zhuǎn)換的代碼如下:
/** 大寫字母轉(zhuǎn)換為小寫 */
- (NSString *)toLower:(NSString *)str {
for (int i = 0; i < str.length; i++) {
if ([str characterAtIndex:i] >= 'A' && [str characterAtIndex:i] <= 'Z') {
char temp = [str characterAtIndex:i] + 32;
NSRange range = NSMakeRange(i, 1);
str = [str stringByReplacingCharactersInRange:range withString:[NSString stringWithFormat:@"%c" , temp]];
}
}
return str;
}
/** 小寫字母轉(zhuǎn)換為大寫 */
-(NSString *)toUpper:(NSString *)str{
for (int i = 0; i < str.length; i++) {
if ([str characterAtIndex:i]>='a'&[str characterAtIndex:i]<='z') {
char temp=[str characterAtIndex:i]-32;
NSRange range=NSMakeRange(i, 1);
str=[str stringByReplacingCharactersInRange:range withString:[NSString stringWithFormat:@"%c",temp]];
}
}
return str;
}
大概就這些步驟了,如有需要可以下載:https://github.com/zhangyqyx/verificationCode
希望大家能提出寶貴的意見(jiàn),可以給我留言,也可以發(fā)郵件到我的郵箱:namezyqyx@163.com
謝謝大家,如果你有更好的想法或文章請(qǐng)告知,不勝感激。