服务器端提供ios图形验证码,IOS 图形验证码

先给大家摆上效果图。

1e94f9c0c8a7?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

1.自定义MQVerCodeInputView类 继承 UIView

MQVerCodeInputView.h 文件中:

#import

typedefvoid(^MQTextViewBlock)(NSString*text);

@interfaceMQVerCodeInputView :UIView

@property(nonatomic,assign)UIKeyboardTypekeyBoardType;

@property(nonatomic,copy)MQTextViewBlockblock;

/*验证码的最大长度*/

@property(nonatomic,assign)NSIntegermaxLenght;

/*未选中下的view的borderColor*/

@property(nonatomic,strong)UIColor*viewColor;

/*选中下的view的borderColor*/

@property(nonatomic,strong)UIColor*viewColorHL;

-(void)mq_verCodeViewWithMaxLenght;

@end

2.UIView+MQ创建类目文件

UIView+MQ.h文件:

#import

@interfaceUIView (MQ)

@property(nonatomic)IBInspectableCGFloatcornerRadius;

/**头像圆角*/

@property(nonatomic)IBInspectableBOOLavatarCorner;

/**边框*/

@property(nonatomic)IBInspectableCGFloatborderWidth;

/**边框颜色*/

@property(nonatomic,strong)IBInspectableUIColor*borderColor;

+ (__kindofUIView*)MQLoadNibView;

- (void)MQSetViewCircleWithBorderWidth:(CGFloat) width andColor:(UIColor*)borColor;

- (void)MQViewSetCornerRadius:(CGFloat)radius;

@end

UIView+MQ.m文件:

#import"UIView+MQ.h"

#import

@implementationUIView (MQ)

- (CGFloat)cornerRadius

{

return[objc_getAssociatedObject(self,@selector(cornerRadius))floatValue];

}

- (void)setCornerRadius:(CGFloat)cornerRadius

{

self.layer.cornerRadius= cornerRadius;

self.layer.masksToBounds= (cornerRadius >0);

}

- (BOOL)avatarCorner{

return[objc_getAssociatedObject(self,@selector(cornerRadius))floatValue] >0;

}

- (void)setAvatarCorner:(BOOL)corner{

if(corner){

self.layer.cornerRadius=CGRectGetWidth(self.frame)/2;

self.layer.masksToBounds= corner;

}

}

- (CGFloat)borderWidth{

return[objc_getAssociatedObject(self,@selector(borderWidth))floatValue];

}

- (void)setBorderWidth:(CGFloat)borderWidth{

self.layer.borderWidth= borderWidth;

self.layer.masksToBounds= (borderWidth >0);

}

- (UIColor*)borderColor{

returnobjc_getAssociatedObject(self,@selector(borderColor));

}

- (void)setBorderColor:(UIColor*)borderColor{

self.layer.borderColor= borderColor.CGColor;

}

+ (__kindofUIView*)MQLoadNibView{

NSString*className =NSStringFromClass([selfclass]);

return[[[UINibnibWithNibName:classNamebundle:nil]instantiateWithOwner:selfoptions:nil]lastObject];

}

-(void)MQViewSetCornerRadius:(CGFloat)radius{

[self.layersetMasksToBounds:YES];

[self.layersetCornerRadius:radius];

}

-(void)MQSetViewCircleWithBorderWidth:(CGFloat) width andColor:(UIColor*)borColor{

[selfMQViewSetCornerRadius:(self.frame.size.height/2)];

self.layer.borderWidth=width;

self.layer.borderColor=[borColorCGColor];

}

@end

3.MQVerCodeInputView.m 文件中:

#import"MQVerCodeInputView.h"

#import"Masonry.h"

#import"UIView+MQ.h"

@interfaceMQVerCodeInputView()

@property(nonatomic,strong)UIView*contairView;

@property(nonatomic,strong)UITextView*textView;

@property(nonatomic,strong)NSMutableArray*viewArr;

@property(nonatomic,strong)NSMutableArray*labelArr;

@property(nonatomic,strong)NSMutableArray*pointlineArr;

@end

@implementationMQVerCodeInputView

-(instancetype)initWithFrame:(CGRect)frame{

self= [superinitWithFrame:frame];

if(self) {

[selfinitDefaultValue];

}

returnself;

}

-(void)initDefaultValue{

//初始化默认值

self.maxLenght=4;

_viewColor= [UIColorcolorWithRed:228/255.0green:228/255.0blue:228/255.0alpha:1];

_viewColorHL= [UIColorcolorWithRed:255/255.0green:70/255.0blue:62/255.0alpha:1];

self.backgroundColor= [UIColorclearColor];

[selfbeginEdit];

}

-(void)mq_verCodeViewWithMaxLenght{

//创建输入验证码view

if(_maxLenght<=0) {

return;

}

if(_contairView) {

[_contairViewremoveFromSuperview];

}

_contairView= [UIViewnew];

_contairView.backgroundColor= [UIColorclearColor];

[selfaddSubview:_contairView];

[_contairViewmas_makeConstraints:^(MASConstraintMaker*make) {

make.height.equalTo(self.mas_height);

make.centerX.equalTo(self);

make.centerY.equalTo(self);

}];

[_contairViewaddSubview:self.textView];

[self.textViewmas_makeConstraints:^(MASConstraintMaker*make) {

make.edges.equalTo(_contairView);

}];

CGFloatpadding = (CGRectGetWidth(self.frame) -_maxLenght*CGRectGetHeight(self.frame))/(_maxLenght-1);

UIView*lastView;

for(inti=0; i

UIView*subView = [UIViewnew];

subView.backgroundColor= [UIColorwhiteColor];

subView.cornerRadius=4;

subView.borderWidth= (0.5);

subView.userInteractionEnabled=NO;

[_contairViewaddSubview:subView];

[subViewmas_makeConstraints:^(MASConstraintMaker*make) {

if(lastView) {

make.left.equalTo(lastView.mas_right).with.offset(padding);

}else{

make.left.equalTo(@(0));

}

make.centerY.equalTo(self.contairView);

make.height.equalTo(self.contairView.mas_height);

make.width.equalTo(self.contairView.mas_height);

}];

UILabel*subLabel = [UILabelnew];

subLabel.font= [UIFontsystemFontOfSize:38];

[subViewaddSubview:subLabel];

[subLabelmas_makeConstraints:^(MASConstraintMaker*make) {

make.centerX.equalTo(subView);

make.centerY.equalTo(subView);

}];

lastView = subView;

UIBezierPath*path = [UIBezierPathbezierPathWithRect:CGRectMake((CGRectGetHeight(self.frame)-2)/2,5,2,(CGRectGetHeight(self.frame)-10))];

CAShapeLayer*line = [CAShapeLayerlayer];

line.path= path.CGPath;

line.fillColor=_viewColorHL.CGColor;

[subView.layeraddSublayer:line];

if(i ==0) {//初始化第一个view为选择状态

[lineaddAnimation:[selfopacityAnimation]forKey:@"kOpacityAnimation"];

line.hidden=NO;

subView.borderColor=_viewColorHL;

}else{

line.hidden=YES;

subView.borderColor=_viewColor;

}

[self.viewArraddObject:subView];

[self.labelArraddObject:subLabel];

[self.pointlineArraddObject:line];

}

[_contairViewmas_makeConstraints:^(MASConstraintMaker*make) {

make.right.equalTo(lastView?lastView.mas_right:@(0));

}];

}

#pragma mark - TextView

-(void)beginEdit{

[self.textViewbecomeFirstResponder];

}

-(void)endEdit{

[self.textViewresignFirstResponder];

}

- (void)textViewDidChange:(UITextView*)textView{

NSLog(@"%@",textView.text);

NSString*verStr = textView.text;

//有空格去掉空格

verStr = [verStrstringByReplacingOccurrencesOfString:@" "withString:@""];

if(verStr.length>=_maxLenght) {

verStr = [verStrsubstringToIndex:_maxLenght];

[selfendEdit];

}

textView.text= verStr;

if(self.block) {

//将textView的值传出去

self.block(verStr);

}

for(inti=0; i

//以text为中介区分

UILabel*label =self.labelArr[i];

if(i

[selfchangeViewLayerIndex:ipointHidden:YES];

label.text= [verStrsubstringWithRange:NSMakeRange(i,1)];

}else{

[selfchangeViewLayerIndex:ipointHidden:i==verStr.length?NO:YES];

if(!verStr&&verStr.length==0) {//textView的text为空的时候

[selfchangeViewLayerIndex:0pointHidden:NO];

}

label.text=@"";

}

}

}

- (void)changeViewLayerIndex:(NSInteger)index pointHidden:(BOOL)hidden{

UIView*view =self.viewArr[index];

view.borderColor= hidden?_viewColor:_viewColorHL;

CAShapeLayer*line =self.pointlineArr[index];

if(hidden) {

[lineremoveAnimationForKey:@"kOpacityAnimation"];

}else{

[lineaddAnimation:[selfopacityAnimation]forKey:@"kOpacityAnimation"];

}

line.hidden= hidden;

}

- (CABasicAnimation*)opacityAnimation {

CABasicAnimation*opacityAnimation = [CABasicAnimationanimationWithKeyPath:@"opacity"];

opacityAnimation.fromValue=@(1.0);

opacityAnimation.toValue=@(0.0);

opacityAnimation.duration=0.9;

opacityAnimation.repeatCount=HUGE_VALF;

opacityAnimation.removedOnCompletion=YES;

opacityAnimation.fillMode=kCAFillModeForwards;

opacityAnimation.timingFunction=[CAMediaTimingFunctionfunctionWithName:kCAMediaTimingFunctionEaseIn];

returnopacityAnimation;

}

#pragma mark --setter&&getter

-(void)setMaxLenght:(NSInteger)maxLenght{

_maxLenght= maxLenght;

}

-(void)setKeyBoardType:(UIKeyboardType)keyBoardType{

_keyBoardType= keyBoardType;

self.textView.keyboardType= keyBoardType;

}

-(void)setViewColor:(UIColor*)viewColor{

_viewColor= viewColor;

}

-(void)setViewColorHL:(UIColor*)viewColorHL{

_viewColorHL= viewColorHL;

}

-(UITextView*)textView{

if(!_textView) {

_textView= [UITextViewnew];

_textView.tintColor= [UIColorclearColor];

_textView.backgroundColor= [UIColorclearColor];

_textView.textColor= [UIColorclearColor];

_textView.delegate=self;

_textView.keyboardType=UIKeyboardTypeDefault;

_textView.autocorrectionType=UITextAutocorrectionTypeNo;//取消自动联想功能

}

return_textView;

}

-(NSMutableArray*)pointlineArr{

if(!_pointlineArr) {

_pointlineArr= [NSMutableArraynew];

}

return_pointlineArr;

}

-(NSMutableArray*)viewArr{

if(!_viewArr) {

_viewArr= [NSMutableArraynew];

}

return_viewArr;

}

-(NSMutableArray*)labelArr{

if(!_labelArr) {

_labelArr= [NSMutableArraynew];

}

return_labelArr;

}

@end

4.自定义文件写好后,在你需要使用的地方调用即刻~

定义外部属性,

MQVerCodeInputView*verView;//自定义验证码

NSString*code;//输入的验证码

点击图形验证码,调用此方法   传入后台请求的图片,转成NSData*类型。

#pragma mark -弹出验证码输入框

-(void)showGraphicalVerificationCode:(NSData*)data{

[self tapBut];

maskView= [[UIViewalloc]initWithFrame:CGRectMake(0,0,SCREENWIDTH,SCREENHEIGHT)];

maskView.backgroundColor= [UIColorblackColor];

maskView.alpha=0.5;

[self.viewaddSubview:maskView];

UITapGestureRecognizer*tapGesture = [[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(tapBut)];

[maskViewaddGestureRecognizer:tapGesture];

imageBtn= [[UIButtonalloc]initWithFrame:CGRectMake(0,0,SCREENWIDTH-50-80,60)];

imageBtn.centerX=maskView.centerX;

imageBtn.centerY=maskView.centerY-80;

imageBtn.layer.masksToBounds=YES;

imageBtn.layer.cornerRadius=4;

[imageBtnsetBackgroundImage:[UIImageimageWithData:data]forState:UIControlStateNormal];

[imageBtnaddTarget:selfaction:@selector(ImageChange:)forControlEvents:UIControlEventTouchUpInside];

[self.viewaddSubview:imageBtn];

__weaktypeof(self) weakSelf =self;

verView= [[MQVerCodeInputViewalloc]initWithFrame:CGRectMake(0,0,SCREENWIDTH-50-80,50)];

verView.maxLenght=4;//最大长度

verView.keyBoardType=UIKeyboardTypePhonePad;

[verViewmq_verCodeViewWithMaxLenght];

verView.block= ^(NSString*text){

NSLog(@"text = %@",text);

if(text.length==4) {

labelText= text;

[weakSelfgetPhoneVerificationCode];

}

};

verView.center=maskView.center;

[self.viewaddSubview:verView];

}

#pragma mark -移除验证码输入框

-(void)tapBut{

[maskViewremoveFromSuperview];

[verViewremoveFromSuperview];

[imageBtnremoveFromSuperview];

}

附上github  图形验证码Demo地址https://github.com/xuliang0712/iOS-.git

如有问题,欢迎交流~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值