国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

利用iOS繪制圖片生成隨機驗證碼示例代碼

2019-10-21 18:50:46
字體:
來源:轉載
供稿:網友

先來看看效果圖

ios,隨機生成驗證碼,ios隨機驗證碼,繪制圖片

實現方法

.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中調用

_codeView = [[CodeView alloc] initWithFrame:CGRectMake(15+(SCREEN_WIDTH-30)/3*2, 75, (SCREEN_WIDTH-30)/3, 39)]; //手勢UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)];[_codeView addGestureRecognizer:tap];[self.view addSubview: _codeView];

手勢事件

- (void)tapClick:(UITapGestureRecognizer*)tap{  [_codeView changeCode];}

總結

以上就是利用iOS繪制圖片隨機驗證碼的全部內容,希望本文的內容對各位iOS開發者們能有所幫助,如果有疑問大家可以留言交流。


注:相關教程知識閱讀請移步到IOS開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 濮阳市| 兰考县| 九寨沟县| 若尔盖县| 马尔康县| 嘉义市| 巴林左旗| 林西县| 石棉县| 德江县| 东城区| 关岭| 弥渡县| 南昌县| 泽州县| 马边| 陕西省| 玉屏| 泊头市| 耒阳市| 河北区| 武川县| 通榆县| 额敏县| 沈阳市| 社旗县| 积石山| 蓬安县| 威远县| 周宁县| 伊春市| 高陵县| 咸阳市| 天门市| 金寨县| 聂荣县| 山阳县| 新邵县| 齐齐哈尔市| 上饶县| 贵定县|