利用iOS繪制圖片生成隨機(jī)驗(yàn)證碼示例代碼
先來看看效果圖
實(shí)現(xiàn)方法
.h文件
@property (nonatomic, retain) NSArray *changeArray; @property (nonatomic, retain) NSMutableString *changeString; @property (nonatomic, retain) UILabel *codeLabel; -(void)changeCode; @end
.m文件
@synthesize changeArray = _changeArray; @synthesize changeString = _changeString; @synthesize codeLabel = _codeLabel; - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { // Initialization code 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)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" sizeWithAttributes:@{NSFontAttributeName:[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 withAttributes:@{NSFontAttributeName:[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
VIewController中調(diào)用
_codeView = [[CodeView alloc] initWithFrame:CGRectMake(15+(SCREEN_WIDTH-30)/3*2, 75, (SCREEN_WIDTH-30)/3, 39)]; //手勢(shì) UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)]; [_codeView addGestureRecognizer:tap]; [self.view addSubview: _codeView];
手勢(shì)事件
- (void)tapClick:(UITapGestureRecognizer*)tap { [_codeView changeCode]; }
總結(jié)
以上就是利用iOS繪制圖片隨機(jī)驗(yàn)證碼的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)各位iOS開發(fā)者們能有所幫助,如果有疑問大家可以留言交流。
相關(guān)文章
iOS實(shí)現(xiàn)卡片式滾動(dòng)效果 iOS實(shí)現(xiàn)電影選片效果
這篇文章主要為大家詳細(xì)介紹了iOS實(shí)現(xiàn)卡片式滾動(dòng)效果,實(shí)現(xiàn)電影選片效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-02-02iOS之單獨(dú)使用UISearchBar創(chuàng)建搜索框的示例
本篇文章主要介紹了iOS之單獨(dú)使用UISearchBar創(chuàng)建搜索框的示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-10-10IOS動(dòng)畫效果源代碼整理(粒子、雪花、火焰、河流、蒸汽)
本篇文章給大家整理的IOS的關(guān)于動(dòng)畫的效果代碼整理,很多效果非常的好看,有興趣的學(xué)下。2018-01-01IOS 自定義UICollectionView的頭視圖或者尾視圖UICollectionReusableView
這篇文章主要介紹了IOS 自定義UICollectionView的頭視圖或者尾視圖UICollectionReusableView的相關(guān)資料,需要的朋友可以參考下2017-01-01iOS實(shí)現(xiàn)應(yīng)用內(nèi)切換語言及字體大?。7挛⑿牛?/a>
這篇文章主要給大家介紹了關(guān)于利用iOS如何實(shí)現(xiàn)應(yīng)用內(nèi)切換語言及字體大小的相關(guān)資料,實(shí)現(xiàn)的效果類似我們經(jīng)常在微信中見到的,文中通過示例代碼介紹的非常詳細(xì),需要的朋友們可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2018-01-01iOS開發(fā)中簡(jiǎn)單實(shí)用的幾個(gè)小技巧
大家可能都知道,在開發(fā)過程中我們總會(huì)遇到各種各樣的小問題,有些小問題并不是十分容易解決。在此我就總結(jié)一下,我在開發(fā)中遇到的各種小問題,以及我的解決方法,也算是些小技巧吧,分享給大家,方便大家在iOS開發(fā)的時(shí)候能夠參考借鑒,下面有需要的朋友一起來看看吧。2016-11-11iOS實(shí)現(xiàn)按鈕點(diǎn)擊選中與被選中切換功能
這篇文章主要介紹了iOS實(shí)現(xiàn)按鈕點(diǎn)擊選中與被選中切換功能,需要的朋友可以參考下2017-07-07