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

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. JavaWeb基础—Servlet

    一.Servlet是什么 是服务器上运行的Java小应用程序,并被称为JavaWeb三大组件之一 通常我们把实现了Servlet的类,称之为Servlet Servlet作用主要是 1.接收请求数据 ...

  2. 复制web项目时注意修改web项目名

    改web项目的项目名可以右击->properties->搜索web找到settings->修改context name

  3. jQuery学习-自定义动画

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  4. 2653: middle

    2653: middle 链接 分析: 二分答案+主席树. 对于中位数的经典做法,就是二分一个数,将小于的变成-1,大于等于的变成+1,那么如果sum>=0(因为+1包括等于),L=mid+1, ...

  5. springboot之rabbitmq

    一.RabbitMQ是实现了高级消息队列协议(AMQP)的开源消息代理软件(亦称面向消息的中间件).RabbitMQ服务器是用Erlang语言编写的,而集群和故障转移是构建在开放电信平台框架上的.所有 ...

  6. win8.1下右下角出现大小写切换状态显示框解决方案

    HKEY_LOCAL_MACHINE\SOFTWARE\Cambridge Silicon Radio\Harmony\Default双击右侧 OSD 将键值改成0 重启机器 成功关闭显示

  7. Java虚拟机笔记(四):垃圾收集器

    前言 前一篇文章介绍了内存的垃圾收集算法,现在介绍下内存回收的具体实现--垃圾收集器. 由于Java虚拟机规范中对垃圾收集器应该如何实现并没有任何规定,因此不同的厂商,不同版本的虚拟机所提供的垃圾收集 ...

  8. css快速入门-引入方式

    一.概述 HTML是骨架.框架CSS是外表.衣服JS是动作.肌肉 css的主要作用是对元素进行渲染.1.找到要操作的标签(选择器)2.对定位的标签进行操作(属性) 二.CSS引入方式 1.行内式 &l ...

  9. ModelForm解密

     一.复用model表和字段 models.py文件 class User(models.Model): username = models.CharField(max_length=32) emai ...

  10. Flutter - > Android dependency 'com.android.support:support-v4' has different version for the compile (26.1.0) and runtime (27.1.1) classpath.

    Launching lib\main.dart on Nokia X6 in debug mode... Initializing gradle... Resolving dependencies.. ...