iOS开发短信验证码封装 方便好用

---恢复内容开始---

1.RootViewControler
//  Copyright © 2016年 Chason. All rights reserved.
//

#import "ViewController.h"
#import "MessageCordView.h"
//手机屏幕的宽和高
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define KScreenHeight [UIScreen mainScreen].bounds.size.height
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    MessageCordView *coreView = [[MessageCordView alloc] initWithFrame:CGRectMake(0, 200, kScreenWidth,50)];
    [self.view addSubview:coreView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

2.封装带输入框的短信验证码

//  Copyright © 2016年 Chason. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface MessageCordView : UIView
@property (nonatomic, strong) UITextField *rechargeField;
@property (nonatomic, strong) UIButton *button;
@property (nonatomic, strong) UILabel *label;
@property (nonatomic, strong) NSTimer *timer;
@end

//  Copyright © 2016年 Chason. All rights reserved.
//

#import "MessageCordView.h"
#import "myView.h"
//手机屏幕的宽和高
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define KScreenHeight [UIScreen mainScreen].bounds.size.height
static int countDownTime = 60;
@implementation MessageCordView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
       
        UIView *rechargeView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        rechargeView.backgroundColor = [UIColor whiteColor];
        [self addSubview:rechargeView];
        
        UILabel *rechargeMoney = [[UILabel alloc] initWithFrame:CGRectMake(15, 10, 100, frame.size.height - 20)];
        rechargeMoney.font = [UIFont systemFontOfSize:16];
        rechargeMoney.textColor = [UIColor grayColor];
        rechargeMoney.text = @"验证码";
        rechargeMoney.font = [UIFont systemFontOfSize:20];
        [rechargeView addSubview:rechargeMoney];
        
        _rechargeField = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, frame.size.width - 230, frame.size.height - 20)];
        _rechargeField.clearButtonMode = UITextFieldViewModeWhileEditing;
        _rechargeField.placeholder = @"请输入验证码";
        _rechargeField.borderStyle = UITextBorderStyleNone;
        [rechargeView addSubview:_rechargeField];

        _button = [myView creatButtonWithFrame:CGRectMake(frame.size.width - 105, 5, 90, frame.size.height - 10) title:@"获取验证码" tag:1000 tintColor:[UIColor whiteColor] backgroundColor:[UIColor colorWithRed:18/255.0  green:129/255.0  blue:201/255.0 alpha:1]];
        _button.layer.cornerRadius = 10;
        _button.font = [UIFont systemFontOfSize:14];
        [_button addTarget:self action:@selector(getVerificationCode) forControlEvents:UIControlEventTouchUpInside];
        [rechargeView addSubview:_button];
    }
    return self;
}

- (void)getVerificationCode
{
    _label = [[UILabel alloc]initWithFrame:self.button.bounds];
    
    _timer = [[NSTimer alloc]init];
    _timer= [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(countDown) userInfo:nil repeats:YES];
    [_timer setFireDate:[NSDate distantPast]];
    //请求服务器发送短信验证码
    //......
}

- (void)countDown
{
    countDownTime--;
    if (countDownTime<=0||countDownTime ==60) {
        _label.hidden = YES;
        _button.hidden = NO;
        _button.userInteractionEnabled = YES;
        [_timer setFireDate:[NSDate distantFuture]];
        countDownTime =60;
    }else{
        _label.hidden = NO;
        _label.text = [NSString stringWithFormat:@"(%ds)重新获取",countDownTime];
        _label.font = [UIFont systemFontOfSize:12];
        _label.backgroundColor = [UIColor colorWithRed:18/255.0  green:129/255.0  blue:201/255.0 alpha:1];
        _label.layer.cornerRadius = 8;
        _label.clipsToBounds = YES;
        _label.textAlignment = NSTextAlignmentCenter;
        _label.textColor= [UIColor whiteColor];
        [_button addSubview:_label];
        _button.userInteractionEnabled = NO;
    }
}

@end

3.创建获取验证码button

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface myView : NSObject

+(UIButton *)creatButtonWithFrame:(CGRect)frame title:(NSString *)title tag:(NSInteger)tag tintColor:(UIColor *)tintColor backgroundColor:(UIColor *)backgroundColor;

@end

#import "myView.h"

@implementation myView

+(UIButton *)creatButtonWithFrame:(CGRect)frame title:(NSString *)title tag:(NSInteger)tag tintColor:(UIColor *)tintColor backgroundColor:(UIColor *)backgroundColor{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:title forState:UIControlStateNormal];
    button.tag = tag;
    button.backgroundColor  = backgroundColor;
    button.tintColor=  tintColor;
    button.frame = frame;
    [button setTitleColor:tintColor forState:UIControlStateNormal];
    return button;
}

@end

如果您觉得博文对您有帮助, 希望您能打发给博主一点茶水钱, 您的支持就是博主最大的动力!

 

---恢复内容结束---

posted @ 2016-04-14 11:46  Chason.cheng  阅读(239)  评论(0编辑  收藏  举报