麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 系統 > iOS > 正文

iOS動態驗證碼實現代碼

2019-10-21 18:41:21
字體:
來源:轉載
供稿:網友

具體代碼如下所示:

//// 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動態驗證碼實現代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對VEVB武林網網站的支持!


注:相關教程知識閱讀請移步到IOS開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 蜜桃视频在线观看免费 | 精品国产一区二区三区久久久蜜月 | 国产美女视频一区 | 成人在线视频精品 | 国产亚洲精品久久久久久久久久 | 神马视频我不卡 | 久久国产精品小视频 | 欧美四级在线观看 | 免费日本一区二区 | www.9191.com| 成人做爰www免费看 欧美精品免费一区二区三区 | 超级av在线 | 欧美 日韩 国产 在线 | 凹凸成人精品亚洲精品密奴 | 免费黄色入口 | 国产成人精品一区在线播放 | 韩国精品视频在线观看 | av免费在线观看不卡 | 日本s级毛片免费观看 | 羞羞答答xxdd在线播放 | 视频一区二区久久 | 日本成人高清视频 | av免费大全 | 欧美综合在线观看 | 久久精品国产亚洲7777小说 | 一级外国毛片 | 永久免费不卡在线观看黄网站 | 久久逼逼 | 特一级黄色毛片 | 日本网站一区二区三区 | 国产99视频精品免视看9 | 2017亚洲男人天堂 | 欧美a级在线免费观看 | 一级一级一级毛片 | 国产免费一区二区三区视频 | 日本va在线观看 | 国产一级二级视频 | 久久国产精品成人免费网站 | 91九色国产视频 | 777zyz色资源站在线观看 | 欧美一级美国一级 |