服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - IOS - iOS动态验证码实现代码

iOS动态验证码实现代码

2021-04-23 18:08ProgramDog_Chris IOS

本文通过实例代码给大家介绍了ios动态验证码的实现方法,代码简单易懂,非常不错,具有参考借鉴价值,需要的朋友参考下吧

具体代码如下所示:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
//
// 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动态验证码实现代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!

原文链接:https://blog.csdn.net/mscinsidious/article/details/79994764

延伸 · 阅读

精彩推荐
  • IOSiOS中tableview 两级cell的展开与收回的示例代码

    iOS中tableview 两级cell的展开与收回的示例代码

    本篇文章主要介绍了iOS中tableview 两级cell的展开与收回的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    J_Kang3862021-04-22
  • IOSiOS通过逆向理解Block的内存模型

    iOS通过逆向理解Block的内存模型

    自从对 iOS 的逆向初窥门径后,我也经常通过它来分析一些比较大的应用,参考一下这些应用中某些功能的实现。这个探索的过程乐趣多多,不仅能满足自...

    Swiftyper12832021-03-03
  • IOSIOS 屏幕适配方案实现缩放window的示例代码

    IOS 屏幕适配方案实现缩放window的示例代码

    这篇文章主要介绍了IOS 屏幕适配方案实现缩放window的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要...

    xiari5772021-06-01
  • IOS解析iOS开发中的FirstResponder第一响应对象

    解析iOS开发中的FirstResponder第一响应对象

    这篇文章主要介绍了解析iOS开发中的FirstResponder第一响应对象,包括View的FirstResponder的释放问题,需要的朋友可以参考下...

    一片枫叶4662020-12-25
  • IOS关于iOS自适应cell行高的那些事儿

    关于iOS自适应cell行高的那些事儿

    这篇文章主要给大家介绍了关于iOS自适应cell行高的那些事儿,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的...

    daisy6092021-05-17
  • IOSIOS开发之字典转字符串的实例详解

    IOS开发之字典转字符串的实例详解

    这篇文章主要介绍了IOS开发之字典转字符串的实例详解的相关资料,希望通过本文能帮助到大家,让大家掌握这样的方法,需要的朋友可以参考下...

    苦练内功5832021-04-01
  • IOSiOS 雷达效果实例详解

    iOS 雷达效果实例详解

    这篇文章主要介绍了iOS 雷达效果实例详解的相关资料,需要的朋友可以参考下...

    SimpleWorld11022021-01-28
  • IOSiOS布局渲染之UIView方法的调用时机详解

    iOS布局渲染之UIView方法的调用时机详解

    在你刚开始开发 iOS 应用时,最难避免或者是调试的就是和布局相关的问题,下面这篇文章主要给大家介绍了关于iOS布局渲染之UIView方法调用时机的相关资料...

    windtersharp7642021-05-04