当前位置: 移动技术网 > IT编程>移动开发>IOS > iOS动态验证码实现代码

iOS动态验证码实现代码

2019年07月24日  | 移动技术网IT编程  | 我要评论

苏州汽车站订票,银角狰的鬃毛,泰坦尼克号的经典台词

具体代码如下所示:

//
// 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动态验证码实现代码,希望对大家有所帮助

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

  • ios uicollectionview实现横向滚动

    现在使用卡片效果的app很多,之前公司让实现一种卡片效果,就写了一篇关于实现卡片的文章。文章最后附有demo实现上我选择了使用uicollectionview ... [阅读全文]
  • iOS UICollectionView实现横向滑动

    本文实例为大家分享了ios uicollectionview实现横向滑动的具体代码,供大家参考,具体内容如下uicollectionview的横向滚动,目前我使... [阅读全文]
  • iOS13适配深色模式(Dark Mode)的实现

    iOS13适配深色模式(Dark Mode)的实现

    好像大概也许是一年前, mac os系统发布了深色模式外观, 看着挺刺激, 时至今日用着也还挺爽的终于, 随着iphone11等新手机的发售, ios 13系统... [阅读全文]
  • ios 使用xcode11 新建项目工程的步骤详解

    ios 使用xcode11 新建项目工程的步骤详解

    xcode11新建项目工程,新增了scenedelegate这个类,转而将原appdelegate负责的对ui生命周期的处理担子接了过来。故此可以理解为:ios... [阅读全文]
  • iOS实现转盘效果

    本文实例为大家分享了ios实现转盘效果的具体代码,供大家参考,具体内容如下demo下载地址: ios转盘效果功能:实现了常用的ios转盘效果,轮盘抽奖效果的实现... [阅读全文]
  • iOS开发实现转盘功能

    本文实例为大家分享了ios实现转盘功能的具体代码,供大家参考,具体内容如下今天给同学们讲解一下一个转盘选号的功能,直接上代码直接看viewcontroller#... [阅读全文]
  • iOS实现轮盘动态效果

    本文实例为大家分享了ios实现轮盘动态效果的具体代码,供大家参考,具体内容如下一个常用的绘图,主要用来打分之类的动画,效果如下。主要是ios的绘图和动画,本来想... [阅读全文]
  • iOS实现九宫格连线手势解锁

    本文实例为大家分享了ios实现九宫格连线手势解锁的具体代码,供大家参考,具体内容如下demo下载地址:效果图:核心代码://// clockview.m// 手... [阅读全文]
  • iOS实现卡片堆叠效果

    本文实例为大家分享了ios实现卡片堆叠效果的具体代码,供大家参考,具体内容如下如图,这就是最终效果。去年安卓5.0发布的时候,当我看到安卓全新的material... [阅读全文]
  • iOS利用余弦函数实现卡片浏览工具

    iOS利用余弦函数实现卡片浏览工具

    本文实例为大家分享了ios利用余弦函数实现卡片浏览工具的具体代码,供大家参考,具体内容如下一、实现效果通过拖拽屏幕实现卡片移动,左右两侧的卡片随着拖动变小,中间... [阅读全文]
验证码:
移动技术网