点击获取短信验证码倒计时(iOS)

今天我们来写一个简单的获取短信验证码功能,希望对你有所帮助哦, 先看一下效果:
Demo图片
  • 写代码的时间到了:
//
//  ViewController.m
//
//  Created by 高雅馨 on 16/8/8.
//  Copyright © 2016年 高雅馨. All rights reserved.
//

#import "ViewController.h"

#define RED_COLOR [UIColor redColor]
#define WIDTH 200
@interface ViewController ()
@property (nonatomic, assign) int x;
@property (nonatomic, assign) int y;
@property (nonatomic, assign) int z;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _x = 0;
    _y = 60;
    _z = 0;
    self.navigationController.navigationBar.translucent = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.tag = 3;
    btn.frame = CGRectMake(50, 50, 100, 40);
    [btn addTarget:self action:@selector(clickBtn) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    
    UILabel * hongse_Label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 40)];
    hongse_Label.tag = 1;
    hongse_Label.text = @"获取验证码";
    hongse_Label.textColor = [UIColor redColor];
    hongse_Label.font = [UIFont systemFontOfSize:18];
    hongse_Label.backgroundColor = [UIColor whiteColor];
    hongse_Label.layer.borderWidth = 1;
    hongse_Label.layer.borderColor = [UIColor blackColor].CGColor;
    hongse_Label.layer.cornerRadius = 10;
    hongse_Label.layer.masksToBounds = YES;
    hongse_Label.textAlignment = NSTextAlignmentCenter;
    [btn addSubview:hongse_Label];
    
    UILabel * huise_Label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 40)];
    huise_Label.tag = 2;
    huise_Label.textColor = [UIColor whiteColor];
    huise_Label.backgroundColor = [UIColor grayColor];
    huise_Label.layer.borderColor = [UIColor grayColor].CGColor;
    huise_Label.layer.borderWidth = 1;
    huise_Label.layer.cornerRadius = 10;
    huise_Label.layer.masksToBounds = YES;
    huise_Label.textAlignment = NSTextAlignmentCenter;
    huise_Label.font = [UIFont systemFontOfSize:18];
    huise_Label.alpha = 0.4;
    [btn addSubview:huise_Label];
    
    huise_Label.hidden = YES;
}
- (void)clickBtn {
    UILabel * hongse = (UILabel *)[self.view viewWithTag:1];
    UILabel * huise = (UILabel *)[self.view viewWithTag:2];
    hongse.hidden = YES;
    huise.hidden = NO;
    UIButton * btn = (UIButton *)[self.view viewWithTag:3];
    btn.userInteractionEnabled = NO;
    if (btn.userInteractionEnabled == NO) {
        if (_z == 0) {
            [self timer];
        }else{
            [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timer) userInfo:nil repeats:NO];
        }
    }
}
- (void)timer {
    _z = 1;
    UILabel * hongse = (UILabel *)[self.view viewWithTag:1];
    UILabel * huise = (UILabel *)[self.view viewWithTag:2];
    UIButton * btn = (UIButton *)[self.view viewWithTag:3];
    huise.text = [NSString stringWithFormat:@"还剩%d秒",_y];
    _y -= 1;
    
    if (_y == -1) {
        btn.userInteractionEnabled = YES;
        _y = 60;
        hongse.hidden = NO;
        huise.hidden = YES;
        _z = 0;
        [self ok];
    }else{
        [self clickBtn];
    }
}
- (void)ok {
    NSLog(@"循环结束");
}
@end

  • 看到这里是不是觉得特别简单, 那么就来赶紧实现吧!🙈
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,366评论 25 709
  • 前言: 点击获取验证码倒计时,在项目中很多都会用到,尤其是现在应用App当中手机验证登录,都会用到。本着封装一个倒...
    麦穗0615阅读 4,097评论 1 7
  • 表单中readonly和disabled属性的区别 设置了readonly的input[text]在提交表单的时候...
    Leamonade阅读 430评论 0 0
  • 夏语 一场雨, 下满了离愁, 窗外路人说着不知明的话语。 叶子堆叠着相遇, 泛黄里有暗黑污垢, 不变的路。...
    青庐阅读 209评论 0 0
  • 我,从小一直就被家长处于放养状态。我爸忙生意,我妈拼事业。所以从小我就不知道,也不在乎学习。小学,初中,高中。一路...
    Wymanorange阅读 319评论 0 1