欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

iOS本地動態(tài)生成驗證碼的方法

 更新時間:2017年01月05日 09:23:20   作者:Lu_Ca  
這篇文章主要介紹了iOS本地動態(tài)生成驗證碼的方法,非常不錯,具有參考借鑒價值,需要的朋友可以參考下

前幾天app注冊被人攻擊了,從網(wǎng)上找了這個先保存下。。。。

用于ios本地動態(tài)生成驗證碼,效果如下:

demo.gif

  • 導(dǎo)入CoreGraphics.framework

用于繪制圖形

  • 封裝UIView,便捷使用,代碼如下:
AuthcodeView.h
#import <UIKit/UIKit.h>
@interface AuthcodeView : UIView
@property (strong, nonatomic) NSArray *dataArray;//字符素材數(shù)組
@property (strong, nonatomic) NSMutableString *authCodeStr;//驗證碼字符串
@end
AuthcodeView.m
#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 6
#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];//獲得隨機(jī)驗證碼
  }
  return self;
}
#pragma mark 獲得隨機(jī)驗證碼
- (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];
  //隨機(jī)從數(shù)組中選取需要個數(shù)的字符串,拼接為驗證碼字符串
  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 點(diǎn)擊界面切換驗證碼
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
  [self getAuthcode];
  //setNeedsDisplay調(diào)用drawRect方法來實現(xiàn)view的繪制
  [self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect
{
  [super drawRect:rect];
  //設(shè)置隨機(jī)背景顏色
  self.backgroundColor = kRandomColor;
  //根據(jù)要顯示的驗證碼字符串,根據(jù)長度,計算每個字符串顯示的位置
  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;
  //依次繪制每一個字符,可以設(shè)置顯示的每個字符的字體大小、顏色、樣式等
  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}];
  }
   //調(diào)用drawRect:之前,系統(tǒng)會向棧中壓入一個CGContextRef,調(diào)用UIGraphicsGetCurrentContext()會取棧頂?shù)腃GContextRef
  CGContextRef context = UIGraphicsGetCurrentContext();
  //設(shè)置線條寬度
  CGContextSetLineWidth(context, kLineWidth);
  //繪制干擾線
  for (int i = 0; i < kLineCount; i++)
  {
    UIColor *color = kRandomColor;
    CGContextSetStrokeColorWithColor(context, color.CGColor);//設(shè)置線條填充色
    //設(shè)置線的起點(diǎn)
    pX = arc4random() % (int)rect.size.width;
    pY = arc4random() % (int)rect.size.height;
    CGContextMoveToPoint(context, pX, pY);
    //設(shè)置線終點(diǎn)
    pX = arc4random() % (int)rect.size.width;
    pY = arc4random() % (int)rect.size.height;
    CGContextAddLineToPoint(context, pX, pY);
    //畫線
    CGContextStrokePath(context);
  }
}
@end
  • 界面添加驗證碼
@interface AuthCodeViewController ()<UITextFieldDelegate, UIAlertViewDelegate>
{
  AuthcodeView *authCodeView;
  UITextField *_input;
}
@end
@implementation AuthCodeViewController
- (void)viewDidLoad {
  [super viewDidLoad];
  // Do any additional setup after loading the view.
  self.view.backgroundColor = [UIColor whiteColor];
  //顯示驗證碼界面
  authCodeView = [[AuthcodeView alloc] initWithFrame:CGRectMake(30, 100, self.view.frame.size.width-60, 40)];
  [self.view addSubview:authCodeView];
  //提示文字
  UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50, 160, self.view.frame.size.width-100, 40)];
  label.text = @"點(diǎn)擊圖片換驗證碼";
  label.font = [UIFont systemFontOfSize:12];
  label.textColor = [UIColor grayColor];
  [self.view addSubview:label];
  //添加輸入框
  _input = [[UITextField alloc] initWithFrame:CGRectMake(30, 220, self.view.frame.size.width-60, 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;
  [self.view addSubview:_input];
}
#pragma mark 輸入框代理,點(diǎn)擊return 按鈕
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
  //判斷輸入的是否為驗證圖片中顯示的驗證碼
  if ([_input.text isEqualToString:authCodeView.authCodeStr])
  {
    //正確彈出警告款提示正確
    UIAlertView *alview = [[UIAlertView alloc] initWithTitle:@"恭喜您 ^o^" message:@"驗證成功" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    [alview show];
  }
  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;
}
#pragma mark 警告框中方法
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
  //清空輸入框內(nèi)容,收回鍵盤
  if (buttonIndex==0)
  {
    _input.text = @"";
    [_input resignFirstResponder];
  }
}

以上所述是小編給大家介紹的iOS本地動態(tài)生成驗證碼的方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

  • IOS程序開發(fā)之禁止輸入表情符號實例代碼

    IOS程序開發(fā)之禁止輸入表情符號實例代碼

    如何禁止輸入表情符號呢?下面腳本之家小編給大家分享IOS程序開發(fā)之禁止輸入表情符號實例代碼,感興趣的朋友參考下吧
    2016-04-04
  • iOS開發(fā)實現(xiàn)計算器功能

    iOS開發(fā)實現(xiàn)計算器功能

    這篇文章主要為大家詳細(xì)介紹了iOS開發(fā)實現(xiàn)計算器功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-10-10
  • iOS開發(fā)中Quartz2D的基本使用方式舉例

    iOS開發(fā)中Quartz2D的基本使用方式舉例

    這篇文章主要介紹了iOS開發(fā)中Quartz2D的基本使用方式舉例,需要的朋友可以參考下
    2015-11-11
  • iOS13原生端適配攻略(推薦)

    iOS13原生端適配攻略(推薦)

    這篇文章主要介紹了iOS13原生端適配攻略(推薦),現(xiàn)匯總一下iOS 13的各種坑,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-10-10
  • iOS之加載Gif圖片的方法

    iOS之加載Gif圖片的方法

    本篇文章主要介紹了iOS之加載Gif圖片,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-11-11
  • 在iOS App中實現(xiàn)地理位置定位的基本方法解析

    在iOS App中實現(xiàn)地理位置定位的基本方法解析

    這篇文章主要介紹了在iOS App中實現(xiàn)地理位置定位的基本方法解析,包括獲取當(dāng)前位置和計算兩點(diǎn)間距離等基本功能的實現(xiàn),需要的朋友可以參考下
    2016-05-05
  • iOS自定義UITabBar中間按鈕

    iOS自定義UITabBar中間按鈕

    這篇文章主要為大家詳細(xì)介紹了iOS自定義UITabBar中間按鈕,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-05-05
  • UITextView實現(xiàn)只允許鏈接交互不允許選擇圖片的方法

    UITextView實現(xiàn)只允許鏈接交互不允許選擇圖片的方法

    這篇文章主要介紹了UITextView實現(xiàn)只允許鏈接交互不允許選擇圖片的方法,文中介紹的非常詳細(xì),相信對大家具有一定的參考價值,需要的朋友們下面來一起看看吧。
    2017-03-03
  • OpenCV??iOS?圖像處理編程入門詳細(xì)教程

    OpenCV??iOS?圖像處理編程入門詳細(xì)教程

    這篇文章主要介紹了OpenCV?iOS?圖像處理編程入門,OpenCV?的應(yīng)用領(lǐng)域非常廣泛,包括圖像拼接、圖像降噪、產(chǎn)品質(zhì)檢、人機(jī)交互、人臉識別、動作識別、動作跟蹤、無人駕駛等,對于圖像處理、人機(jī)交互及機(jī)器學(xué)習(xí)算法感興趣的可以選擇一個方向進(jìn)行深入的研究
    2022-07-07
  • iOS開發(fā)之image圖片壓縮及壓縮成指定大小的兩種方法

    iOS開發(fā)之image圖片壓縮及壓縮成指定大小的兩種方法

    這篇文章主要介紹了iOS開發(fā)之image圖片壓縮及壓縮成指定大小的兩種方法,需要的朋友可以參考下
    2017-11-11

最新評論