欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

iOS动态验证码实现代码

程序员文章站 2023-12-18 18:32:46
具体代码如下所示: // // authcodeview.h // bsbracelet // // created by christopher on...

具体代码如下所示:

//
// authcodeview.h
// bsbracelet
//
// created by christopher on 17/5/16.
// copyright © 2017年 ztracy. all rights reserved.
//
#import <uikit/uikit.h>
@interface authcodeview : uiview
@property (strong, nonatomic) nsarray *dataarray;//字符素材数组
@property (strong, nonatomic) nsmutablestring *authcodestr;//验证码字符串
@end
//
// authcodeview.m
// bsbracelet
//
// created by christopher on 17/5/16.
// copyright © 2017年 ztracy. all rights reserved.
//
#import "authcodeview.h"
#define krandomcolor [uicolor colorwithred:arc4random() % 256 / 256.0 green:arc4random() % 256 / 256.0 blue:arc4random() % 256 / 256.0 alpha:1.0];
#define klinecount 6
#define klinewidth 1.0
#define kcharcount 4
#define kfontsize [uifont systemfontofsize:arc4random() % 5 + 15]
@implementation authcodeview
/*
// only override drawrect: if you perform custom drawing.
// an empty implementation adversely affects performance during animation.
- (void)drawrect:(cgrect)rect {
  // drawing code
}
*/
- (instancetype)initwithframe:(cgrect)frame
{
  self = [super initwithframe:frame];
  if (self)
  {
    self.layer.cornerradius = 5.0f;
    self.layer.maskstobounds = yes;
    self.backgroundcolor = krandomcolor;
    [self getauthcode];//获得随机验证码
  }
  return self;
}
#pragma mark 获得随机验证码
- (void)getauthcode
{
  //字符串素材
  _dataarray = [[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];
  _authcodestr = [[nsmutablestring alloc] initwithcapacity:kcharcount];
  //随机从数组中选取需要个数的字符串,拼接为验证码字符串
  for (int i = 0; i < kcharcount; i++)
  {
    nsinteger index = arc4random() % (_dataarray.count-1);
    nsstring *tempstr = [_dataarray objectatindex:index];
    _authcodestr = (nsmutablestring *)[_authcodestr stringbyappendingstring:tempstr];
  }
}
#pragma mark 点击界面切换验证码
- (void)touchesbegan:(nsset *)touches withevent:(uievent *)event
{
  [self getauthcode];
  //setneedsdisplay调用drawrect方法来实现view的绘制
  [self setneedsdisplay];
}
- (void)drawrect:(cgrect)rect
{
  [super drawrect:rect];
  //设置随机背景颜色
  self.backgroundcolor = krandomcolor;
  //根据要显示的验证码字符串,根据长度,计算每个字符串显示的位置
  nsstring *text = [nsstring stringwithformat:@"%@",_authcodestr];
  cgsize csize = [@"a" 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:kfontsize}];
  }
  //调用drawrect:之前,系统会向栈中压入一个cgcontextref,调用uigraphicsgetcurrentcontext()会取栈顶的cgcontextref
  cgcontextref context = uigraphicsgetcurrentcontext();
  //设置线条宽度
  cgcontextsetlinewidth(context, klinewidth);
  //绘制干扰线
  for (int i = 0; i < klinecount; i++)
  {
    uicolor *color = krandomcolor;
    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
使用
-(void)recordbtnclick:(uibutton *)recordbtn
{
  domodelview = [[uiview alloc]initwithframe:cgrectmake(0,0,kscreenwidth,kscreenheight)];
  domodelview.backgroundcolor = [uicolor colorwithred:0.3 green:0.3 blue:0.3 alpha:0.6];
  authbgview = [[uiview alloc]initwithframe:cgrectmake(30, 120, self.view.frame.size.width-60, 220)];
  authbgview.backgroundcolor = [uicolor whitecolor]; //显示验证码界面
  authbgview.layer.cornerradius = 8;
  authbgview.layer.maskstobounds = yes;
  authcodeview = [[authcodeview alloc] initwithframe:cgrectmake(10, 10, kscreenwidth-80, 45)];
  [authbgview addsubview:authcodeview];
    //提示文字
  uilabel *label = [[uilabel alloc] initwithframe:cgrectmake(10, 65, kscreenwidth-80, 40)];
  label.text = @"点击图片换验证码";
  label.font = [uifont systemfontofsize:15];
  label.textcolor = [uicolor graycolor];
  [authbgview addsubview:label];
  //添加输入框
  _input = [[uitextfield alloc] initwithframe:cgrectmake(10, 120, kscreenwidth-80, 40)];
  _input.layer.bordercolor = [uicolor lightgraycolor].cgcolor;
  _input.layer.borderwidth = 2.0;
  _input.layer.cornerradius = 5.0;
  _input.font = [uifont systemfontofsize:21];
  _input.placeholder = @"请输入验证码!";
  _input.clearbuttonmode = uitextfieldviewmodewhileediting;
  _input.backgroundcolor = [uicolor clearcolor];
  _input.textalignment = nstextalignmentcenter;
  _input.returnkeytype = uireturnkeydone;
  _input.delegate = self;
  [authbgview addsubview:_input];
  uibutton *closebtn = [[uibutton alloc] initwithframe:cgrectmake(10,170, kscreenwidth-80, 40)];
  [closebtn addtarget:self action:@selector(removeview) forcontrolevents:uicontroleventtouchupinside];
  closebtn.backgroundcolor = rgbcolor(34,151,216);
  [closebtn settitle: @"关闭" forstate: uicontrolstatenormal];
  [authbgview addsubview:closebtn];
  [domodelview addsubview:authbgview];
   [self.view addsubview:domodelview];
//  invoicerecordviewcontroller *invoicerecordvc = [[invoicerecordviewcontroller alloc] initwithnibname:@"invoicerecordviewcontroller" bundle:nil];
//  [self.navigationcontroller pushviewcontroller:invoicerecordvc animated:yes];
}
#pragma mark 输入框代理,点击return 按钮
- (bool)textfieldshouldreturn:(uitextfield *)textfield
{
  //判断输入的是否为验证图片中显示的验证码
  if([_input.text compare:authcodeview.authcodestr options:nscaseinsensitivesearch |nsnumericsearch] ==nsorderedsame)
  {
    //正确弹出警告款提示正确
    //    uialertview *alview = [[uialertview alloc] initwithtitle:@"恭喜您 ^o^" message:@"验证成功" delegate:self cancelbuttontitle:@"ok" otherbuttontitles:nil, nil];
    //    [alview show];
    [domodelview removefromsuperview];
    invoicerecordviewcontroller *invoicerecordvc = [[invoicerecordviewcontroller alloc] initwithnibname:@"invoicerecordviewcontroller" bundle:nil];
    [self.navigationcontroller pushviewcontroller:invoicerecordvc animated:yes];
  }
  else
  {
    //验证不匹配,验证码和输入框抖动
    cakeyframeanimation *anim = [cakeyframeanimation animationwithkeypath:@"transform.translation.x"];
    anim.repeatcount = 1;
    anim.values = @[@-20,@20,@-20];
    //    [authcodeview.layer addanimation:anim forkey:nil];
    [_input.layer addanimation:anim forkey:nil];
  }
  return yes;
}

总结

以上所述是小编给大家介绍的ios动态验证码实现代码,希望对大家有所帮助

上一篇:

下一篇: