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

用于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 點擊界面切換驗證碼
- (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è)置線的起點
        pX = arc4random() % (int)rect.size.width;
        pY = arc4random() % (int)rect.size.height;
        CGContextMoveToPoint(context, pX, pY);
        //設(shè)置線終點
        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 = @"點擊圖片換驗證碼";
    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 輸入框代理,點擊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];
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市托呕,隨后出現(xiàn)的幾起案子含蓉,更是在濱河造成了極大的恐慌,老刑警劉巖项郊,帶你破解...
    沈念sama閱讀 221,548評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件馅扣,死亡現(xiàn)場離奇詭異,居然都是意外死亡着降,警方通過查閱死者的電腦和手機(jī)差油,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,497評論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來任洞,“玉大人蓄喇,你說我怎么就攤上這事〗惶停” “怎么了妆偏?”我有些...
    開封第一講書人閱讀 167,990評論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長盅弛。 經(jīng)常有香客問我钱骂,道長叔锐,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,618評論 1 296
  • 正文 為了忘掉前任罐柳,我火速辦了婚禮掌腰,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘张吉。我一直安慰自己,他們只是感情好催植,可當(dāng)我...
    茶點故事閱讀 68,618評論 6 397
  • 文/花漫 我一把揭開白布肮蛹。 她就那樣靜靜地躺著,像睡著了一般创南。 火紅的嫁衣襯著肌膚如雪伦忠。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,246評論 1 308
  • 那天稿辙,我揣著相機(jī)與錄音昆码,去河邊找鬼。 笑死邻储,一個胖子當(dāng)著我的面吹牛赋咽,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播吨娜,決...
    沈念sama閱讀 40,819評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼脓匿,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了宦赠?” 一聲冷哼從身側(cè)響起陪毡,我...
    開封第一講書人閱讀 39,725評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎勾扭,沒想到半個月后毡琉,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,268評論 1 320
  • 正文 獨居荒郊野嶺守林人離奇死亡妙色,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,356評論 3 340
  • 正文 我和宋清朗相戀三年桅滋,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片燎斩。...
    茶點故事閱讀 40,488評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡虱歪,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出栅表,到底是詐尸還是另有隱情笋鄙,我是刑警寧澤,帶...
    沈念sama閱讀 36,181評論 5 350
  • 正文 年R本政府宣布怪瓶,位于F島的核電站萧落,受9級特大地震影響践美,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜找岖,卻給世界環(huán)境...
    茶點故事閱讀 41,862評論 3 333
  • 文/蒙蒙 一陨倡、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧许布,春花似錦兴革、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,331評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至袁余,卻和暖如春擎勘,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背颖榜。 一陣腳步聲響...
    開封第一講書人閱讀 33,445評論 1 272
  • 我被黑心中介騙來泰國打工棚饵, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人掩完。 一個月前我還...
    沈念sama閱讀 48,897評論 3 376
  • 正文 我出身青樓噪漾,卻偏偏與公主長得像,于是被迫代替她去往敵國和親藤为。 傳聞我的和親對象是個殘疾皇子怪与,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,500評論 2 359

推薦閱讀更多精彩內(nèi)容