IOS开发中绘制图片生成随机验证码的操作步骤
来源:爱站网时间:2020-12-12编辑:网友分享
验证码的出现让captcha从随机生成的数字或符号串中生成一张图片,而程序员需要添加一些干扰素再进行验证,现在爱站技术频道先就为大家介绍IOS开发中绘制图片生成随机验证码的操作步骤,希望能帮到您。
验证码的出现让captcha从随机生成的数字或符号串中生成一张图片,而程序员需要添加一些干扰素再进行验证,现在爱站技术频道先就为大家介绍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
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中处理按钮单击事件
