麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁(yè) > 系統(tǒng) > iOS > 正文

iOS生成圖片數(shù)字字母驗(yàn)證效果

2019-10-21 18:39:18
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

本文實(shí)例為大家分享了iOS生成圖片數(shù)字字母驗(yàn)證的具體代碼,供大家參考,具體內(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]    //顯示一個(gè)隨機(jī)驗(yàn)證碼    [self changeCaptcha];  }  return self;}#pragma mark 更換驗(yàn)證碼,得到更換的驗(yàn)證碼的字符串-(void)changeCaptcha{  //<一>從字符數(shù)組中隨機(jī)抽取相應(yīng)數(shù)量的字符,組成驗(yàn)證碼字符串  //數(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:來(lái)設(shè)置,好處是當(dāng)元素個(gè)數(shù)不超過(guò)容量時(shí),添加元素不需要重新分配內(nèi)存  NSMutableString *getStr = [[NSMutableString alloc] initWithCapacity:kCharCount];  self.changeString = [[NSMutableString alloc] initWithCapacity:kCharCount];  //隨機(jī)從數(shù)組中選取需要個(gè)數(shù)的字符,然后拼接為一個(gè)字符串  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 點(diǎn)擊view時(shí)調(diào)用,因?yàn)楫?dāng)前類(lèi)自身就是UIView,點(diǎn)擊更換驗(yàn)證碼可以直接寫(xiě)到這個(gè)方法中,不用再額外添加手勢(shì)-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{  //點(diǎn)擊界面,切換驗(yàn)證碼  [self changeCaptcha];  //setNeedsDisplay調(diào)用drawRect方法來(lái)實(shí)現(xiàn)view的繪制  [self setNeedsDisplay];}#pragma mark 繪制界面(1.UIView初始化后自動(dòng)調(diào)用; 2.調(diào)用setNeedsDisplay方法時(shí)會(huì)自動(dòng)調(diào)用)- (void)drawRect:(CGRect)rect {  // 重寫(xiě)父類(lèi)方法,首先要調(diào)用父類(lèi)的方法  [super drawRect:rect];  //設(shè)置隨機(jī)背景顏色  self.backgroundColor = kRandomColor;  //獲得要顯示驗(yàn)證碼字符串,根據(jù)長(zhǎng)度,計(jì)算每個(gè)字符顯示的大概位置  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;  //依次繪制每一個(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}];  }    //調(diào)用drawRect:之前,系統(tǒng)會(huì)向棧中壓入一個(gè)CGContextRef,調(diào)用UIGraphicsGetCurrentContext()會(huì)取棧頂?shù)腃GContextRef    CGContextRef context = UIGraphicsGetCurrentContext();    //設(shè)置畫(huà)線寬度    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);      //畫(huà)線      CGContextStrokePath(context);    }}@end

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持VEVB武林網(wǎng)。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到IOS開(kāi)發(fā)頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 黄色特级 | 国产精品久久77777 | 蜜桃一本色道久久综合亚洲精品冫 | 成人在线视频在线观看 | 久久金品 | 免费a视频 | 久久精品亚洲国产奇米99 | 在线播放免费视频 | 国产午夜亚洲精品理论片大丰影院 | 欧美黄色大片免费观看 | 性猛aa久久久 | av播播 | 激情综合网俺也去 | 国产18成人免费视频 | 黄色大片网 | 91社影院在线观看 | 国产成人在线一区二区 | 日韩中字幕| 51色视频| 手机av免费电影 | 国产乱一区二区三区视频 | 色视频在线播放 | 日韩黄色片免费看 | 久久免费观看一级毛片 | 午夜爽爽爽男女免费观看hd | 亚洲精品一区二区三区在线看 | 国产一区二区影视 | 精品人伦一区二区三区蜜桃网站 | av在线免费观看播放 | 久久综合精品视频 | 中文日韩在线视频 | 亚洲卡通动漫在线观看 | av之家在线观看 | 国产精品视频导航 | 香蕉国产片 | 欧美日韩高清一区二区三区 | 国产日韩精品欧美一区视频 | 国产精品久久久不卡 | 成人一级视频 | 色播亚洲 | 成人国产精品一区二区毛片在线 |