iOS生成圖片數(shù)字字母驗證效果
更新時間:2018年11月13日 11:35:00 作者:書弋江山
這篇文章主要為大家詳細(xì)介紹了iOS生成圖片數(shù)字字母驗證效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了iOS生成圖片數(shù)字字母驗證的具體代碼,供大家參考,具體內(nèi)容如下
直接上代碼,注釋很詳細(xì)
#import "CaptchaView.h"
#define kRandomColor [UIColor colorWithRed:arc4random() % 256 / 256.0 green:arc4random() % 256 / 256.0 blue:arc4random() % 256 / 256.0 alpha:1.0];
//#define kRandomColor [UIColor grayColor];
#define kLineCount 6
#define kLineWidth 1.0
#define kCharCount 4
#define kFontSize [UIFont systemFontOfSize:arc4random() % 5 + 15]
@implementation CaptchaView
@synthesize changeString,changeArray;
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
self.layer.cornerRadius = 5.0; //設(shè)置layer圓角半徑
self.layer.masksToBounds = YES; //隱藏邊界
self.backgroundColor = kRandomColor;
// [UIColor grayColor]
//顯示一個隨機(jī)驗證碼
[self changeCaptcha];
}
return self;
}
#pragma mark 更換驗證碼,得到更換的驗證碼的字符串
-(void)changeCaptcha
{
//<一>從字符數(shù)組中隨機(jī)抽取相應(yīng)數(shù)量的字符,組成驗證碼字符串
//數(shù)組中存放的是全部可選的字符,可以是字母,也可以是中文
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];
//如果能確定最大需要的容量,使用initWithCapacity:來設(shè)置,好處是當(dāng)元素個數(shù)不超過容量時,添加元素不需要重新分配內(nèi)存
NSMutableString *getStr = [[NSMutableString alloc] initWithCapacity:kCharCount];
self.changeString = [[NSMutableString alloc] initWithCapacity:kCharCount];
//隨機(jī)從數(shù)組中選取需要個數(shù)的字符,然后拼接為一個字符串
for(int i = 0; i < kCharCount; i++)
{
NSInteger index = arc4random() % ([self.changeArray count] - 1);
getStr = [self.changeArray objectAtIndex:index];
self.changeString = (NSMutableString *)[self.changeString stringByAppendingString:getStr];
}
}
#pragma mark 點擊view時調(diào)用,因為當(dāng)前類自身就是UIView,點擊更換驗證碼可以直接寫到這個方法中,不用再額外添加手勢
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//點擊界面,切換驗證碼
[self changeCaptcha];
//setNeedsDisplay調(diào)用drawRect方法來實現(xiàn)view的繪制
[self setNeedsDisplay];
}
#pragma mark 繪制界面(1.UIView初始化后自動調(diào)用; 2.調(diào)用setNeedsDisplay方法時會自動調(diào)用)
- (void)drawRect:(CGRect)rect {
// 重寫父類方法,首先要調(diào)用父類的方法
[super drawRect:rect];
//設(shè)置隨機(jī)背景顏色
self.backgroundColor = kRandomColor;
//獲得要顯示驗證碼字符串,根據(jù)長度,計算每個字符顯示的大概位置
NSString *text = [NSString stringWithFormat:@"%@",self.changeString];
CGSize cSize = [@"S" sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:20.0]}];
int width = rect.size.width / text.length - cSize.width;
int height = rect.size.height - cSize.height;
CGPoint point;
//依次繪制每一個字符,可以設(shè)置顯示的每個字符的字體大小、顏色、樣式等
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)會向棧中壓入一個CGContextRef,調(diào)用UIGraphicsGetCurrentContext()會取棧頂?shù)腃GContextRef
CGContextRef context = UIGraphicsGetCurrentContext();
//設(shè)置畫線寬度
CGContextSetLineWidth(context, kLineWidth);
//繪制干擾的彩色直線
for(int i = 0; i < kLineCount; i++)
{
//設(shè)置線的隨機(jī)顏色
UIColor *color = kRandomColor;
CGContextSetStrokeColorWithColor(context, [color CGColor]);
//設(shè)置線的起點
pX = arc4random() % (int)rect.size.width;
pY = arc4random() % (int)rect.size.height;
CGContextMoveToPoint(context, pX, pY);
//設(shè)置線終點
pX = arc4random() % (int)rect.size.width;
pY = arc4random() % (int)rect.size.height;
CGContextAddLineToPoint(context, pX, pY);
//畫線
CGContextStrokePath(context);
}
}
@end
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
深入學(xué)習(xí)iOS7自定義導(dǎo)航轉(zhuǎn)場動畫
這篇文章主要為大家詳細(xì)介紹了iOS7自定義導(dǎo)航轉(zhuǎn)場動畫的相關(guān)資料,感興趣的小伙伴們可以參考一下2016-02-02
iOS AVPlayer切換播放源實現(xiàn)連續(xù)播放和全屏切換的方法
這篇文章主要給大家介紹了關(guān)于iOS中AVPlayer切換播放源實現(xiàn)連續(xù)播放和全屏切換的方法,文中給出了詳細(xì)的示例代碼供大家參考學(xué)習(xí),對大家具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起看看吧。2017-05-05
IOS 改變導(dǎo)航欄返回按鈕的標(biāo)題實例詳解
這篇文章主要介紹了IOS 改變導(dǎo)航欄返回按鈕的標(biāo)題實例詳解的相關(guān)資料,需要的朋友可以參考下2017-04-04

