iOS 生成图片验证码绘制实例代码

 更新时间:2017年05月20日 11:33:27   作者:寒田半_亩  
本篇文章主要介绍了iOS 图片验证码绘制实例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

登录注册时用的验证码效果图


ViewDidload调用即可

 _pooCodeView = [[PooCodeView alloc] initWithFrame:CGRectMake(50, 100, 82, 32)];
 UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)];
 [_pooCodeView addGestureRecognizer:tap];
[self.view addSubview:_pooCodeView];

#import <UIKit/UIKit.h>
@interface PooCodeView : UIView
@property (nonatomic, retain) NSArray *changeArray;
@property (nonatomic, retain) NSMutableString *changeString;
@property (nonatomic, retain) UILabel *codeLabel;
-(void)changeCode;
@end

#import "PooCodeView.h"
@implementation PooCodeView
@synthesize changeArray = _changeArray;
@synthesize changeString = _changeString;
@synthesize codeLabel = _codeLabel;

- (id)initWithFrame:(CGRect)frame
{
  self = [super initWithFrame:frame];
  if (self) {
    // Initialization code

//    self.layer.cornerRadius = 5.0;
//    self.layer.masksToBounds = YES;
    float red = arc4random() % 100 / 100.0;
    float green = arc4random() % 100 / 100.0;
    float blue = arc4random() % 100 / 100.0;
    UIColor *color = [UIColor colorWithRed:red green:green blue:blue alpha:0.2];
    self.backgroundColor = color;
    [self change];
  }
  return self;
}
 //-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
//{
//  [self change];
//  [self setNeedsDisplay];
//}

-(void)changeCode{
[self change];
[self setNeedsDisplay];
}

 - (void)change
 {
   self.changeArray = [[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];

  NSMutableString *getStr = [[NSMutableString alloc] initWithCapacity:5];

  self.changeString = [[NSMutableString alloc] initWithCapacity:6];
  for(NSInteger i = 0; i < 4; i++)
  {
    NSInteger index = arc4random() % ([self.changeArray count] - 1);
    getStr = [self.changeArray objectAtIndex:index];

    self.changeString = (NSMutableString *)[self.changeString stringByAppendingString:getStr];
  }
  }

 - (void)drawRect:(CGRect)rect {
[super drawRect:rect];

  float red = arc4random() % 100 / 100.0;
  float green = arc4random() % 100 / 100.0;
  float blue = arc4random() % 100 / 100.0;

  UIColor *color = [UIColor colorWithRed:red green:green blue:blue alpha:0.5];
  [self setBackgroundColor:color];

  NSString *text = [NSString stringWithFormat:@"%@",self.changeString];
  CGSize cSize = [@"S" sizeWithFont:[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 withFont:[UIFont systemFontOfSize:20]];
  }

  CGContextRef context = UIGraphicsGetCurrentContext();
  CGContextSetLineWidth(context, 1.0);
  for(int cout = 0; cout < 10; cout++)
  {
    red = arc4random() % 100 / 100.0;
    green = arc4random() % 100 / 100.0;
    blue = arc4random() % 100 / 100.0;
    color = [UIColor colorWithRed:red green:green blue:blue alpha:0.2];
    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

github下载地址

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • iOS开发之如何通过PUT请求上传数据

    iOS开发之如何通过PUT请求上传数据

    众所周知一般的服务器上传数据都是用POST请求,这样通过AFNetworking的POST请求稳稳的,但是有一天遇到一个问题,服务器上传数据用的是PUT请求,发现用AFNetworking并不是那么好用,下面这篇文章就来讲一下如何通过PUT请求上传数据。有需要的朋友们可以参考借鉴。
    2016-11-11
  • ios的手势操作之UIGestureRecognizer浅析(推荐)

    ios的手势操作之UIGestureRecognizer浅析(推荐)

    本篇文章主要介绍了ios的手势操作之UIGestureRecognizer浅析,小编觉得挺不错的,现在分享给大家,也给大家做个参考。
    2016-12-12
  • iOS开发之自定义UITextField的方法

    iOS开发之自定义UITextField的方法

    UITextField是IOS开发中用户交互中重要的一个控件,常被用来做账号密码框,输入信息框等。本文给大家介绍iOS开发之自定义UITextField的方法,感兴趣的朋友一起学习吧
    2016-05-05
  • Objective-C中的重载和重写详解

    Objective-C中的重载和重写详解

    这篇文章主要介绍了Objective-C中的重载和重写详解的相关资料,开发IOS APP的好多朋友很容易搞错重载和重写,这里就详细介绍下,需要的朋友可以参考下
    2016-11-11
  • iOS实现抖音点赞动画效果

    iOS实现抖音点赞动画效果

    这篇文章主要为大家详细介绍了iOS实现抖音点赞动画效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-01-01
  • 实例讲解iOS应用开发中UIPickerView滚动选择栏的用法

    实例讲解iOS应用开发中UIPickerView滚动选择栏的用法

    这篇文章主要介绍了iOS应用开发中UIPickerView滚动选择栏的用法,示例代码基于传统的Objective-C,需要的朋友可以参考下
    2016-04-04
  • react-native中AsyncStorage实例详解

    react-native中AsyncStorage实例详解

    这篇文章主要介绍了react-native中AsyncStorage实例详解的相关资料,需要的朋友可以参考下
    2017-03-03
  • IOS开发中取消文本框输入时的小键盘

    IOS开发中取消文本框输入时的小键盘

    这篇文章主要介绍了IOS开发中取消文本框输入时的小键盘,需要的朋友可以参考下
    2015-05-05
  • iOS开发之触摸事件

    iOS开发之触摸事件

    iOS设备都是可以多点触摸的,是指手指放在iOS设备的屏幕上从屏幕上拖动或抬起。系统当前视图响应触摸事件,若无响应则向上层传递,构成响应者链。触摸事件的函数有4个。
    2016-04-04
  • 详解ios监听reloadData刷新列表完毕的时机

    详解ios监听reloadData刷新列表完毕的时机

    这篇文章主要介绍了详解ios监听reloadData刷新列表完毕的时机,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-11-11

最新评论