先給大家擺上效果圖。
1.自定義MQVerCodeInputView類 繼承 UIView
MQVerCodeInputView.h 文件中:
#import <UIKit/UIKit.h>
typedefvoid(^MQTextViewBlock)(NSString*text);
@interfaceMQVerCodeInputView :UIView
@property(nonatomic,assign)UIKeyboardTypekeyBoardType;
@property(nonatomic,copy)MQTextViewBlockblock;
/*驗(yàn)證碼的最大長度*/
@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創(chuàng)建類目文件
UIView+MQ.h文件:
#import <UIKit/UIKit.h>
@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 <objc/runtime.h>
@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{
//初始化默認(rèn)值
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{
//創(chuàng)建輸入驗(yàn)證碼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為選擇狀態(tài)
[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為中介區(qū)分
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;//取消自動聯(lián)想功能
}
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.自定義文件寫好后绸狐,在你需要使用的地方調(diào)用即刻~
定義外部屬性,
MQVerCodeInputView*verView;//自定義驗(yàn)證碼
NSString*code;//輸入的驗(yàn)證碼
點(diǎn)擊圖形驗(yàn)證碼突琳,調(diào)用此方法 ? 傳入后臺請求的圖片劫窒,轉(zhuǎn)成NSData*類型。
#pragma mark -彈出驗(yàn)證碼輸入框
-(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 -移除驗(yàn)證碼輸入框
-(void)tapBut{
[maskViewremoveFromSuperview];
[verViewremoveFromSuperview];
[imageBtnremoveFromSuperview];
}
附上github ?圖形驗(yàn)證碼Demo地址https://github.com/xuliang0712/iOS-.git
如有問題冠息,歡迎交流~