iOS 生成圖片驗(yàn)證碼繪制實(shí)例代碼
更新時(shí)間:2017年05月20日 11:33:27 作者:寒田半_畝
本篇文章主要介紹了iOS 圖片驗(yàn)證碼繪制實(shí)例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
登錄注冊(cè)時(shí)用的驗(yàn)證碼效果圖
ViewDidload調(diào)用即可
_pooCodeView = [[PooCodeView alloc] initWithFrame:CGRectMake(50, 100, 82, 32)]; UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)]; [_pooCodeView addGestureRecognizer:tap]; [self.view addSubview:_pooCodeView]; #import <UIKit/UIKit.h> @interface PooCodeView : UIView @property (nonatomic, retain) NSArray *changeArray; @property (nonatomic, retain) NSMutableString *changeString; @property (nonatomic, retain) UILabel *codeLabel; -(void)changeCode; @end #import "PooCodeView.h" @implementation PooCodeView @synthesize changeArray = _changeArray; @synthesize changeString = _changeString; @synthesize codeLabel = _codeLabel; - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { // Initialization code // self.layer.cornerRadius = 5.0; // self.layer.masksToBounds = YES; 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; } //-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event //{ // [self change]; // [self setNeedsDisplay]; //} -(void)changeCode{ [self change]; [self setNeedsDisplay]; } - (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]; 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]; } } - (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]; 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]]; } 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); } } @end
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
詳解iOS應(yīng)用UI開發(fā)中的九宮格坐標(biāo)計(jì)算與字典轉(zhuǎn)換模型
這篇文章主要介紹了iOS應(yīng)用UI開發(fā)中的九宮格坐標(biāo)計(jì)算與字典轉(zhuǎn)換模型,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2016-01-01iOS密碼在進(jìn)入后臺(tái)1小時(shí)后重新設(shè)置
這篇文章主要介紹了iOS密碼在進(jìn)入后臺(tái)1小時(shí)后重新設(shè)置的相關(guān)資料,需要的朋友可以參考下2017-08-08iOS應(yīng)用開發(fā)中UITabBarController標(biāo)簽欄控制器使用進(jìn)階
這篇文章主要介紹了iOS應(yīng)用開發(fā)中UITabBarController標(biāo)簽欄控制器的使用進(jìn)階,實(shí)例代碼為傳統(tǒng)的Objective-C,需要的朋友可以參考下2016-03-03詳解iOS webview加載時(shí)序和緩存問題總結(jié)
本篇文章主要介紹了iOS webview加載時(shí)序和緩存問題總結(jié) ,這兩天學(xué)習(xí)了Vue.js 感覺組件這個(gè)地方知識(shí)點(diǎn)挺多的,而且很重要,所以,今天添加一點(diǎn)小筆記。2017-09-09解析iOS應(yīng)用的UI開發(fā)中懶加載和xib的簡(jiǎn)單使用方法
這篇文章主要介紹了解析iOS應(yīng)用的UI開發(fā)中懶加載和xib的簡(jiǎn)單使用方法,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2016-01-01