美文网首页Iosios程序员
点击获取短信验证码倒计时(iOS)

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

作者: Devil雅馨 | 来源:发表于2016-08-12 09:45 被阅读1252次
今天我们来写一个简单的获取短信验证码功能,希望对你有所帮助哦, 先看一下效果:
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

  • 看到这里是不是觉得特别简单, 那么就来赶紧实现吧!🙈

相关文章

网友评论

  • 小猿猴:艾玛!你才学大半年的咋啥都会啊! :scream:
    Devil雅馨:@小猿猴 哈哈 O(∩_∩)O谢谢 :smile:
  • zhanghengiOS:变量名还是起的有实际意义一些吧,相互学习,相互交流
    Devil雅馨:@zhanghengiOS 好的呢
  • 米饭TT:最好别用这种计时
    Devil雅馨:@米饭TT 非常感谢 :kissing_heart:
    米饭TT:@Devil雅馨 http://blog.csdn.net/yuquan0821/article/details/16843195 你可以看看这个,计时比较准
    Devil雅馨:@米饭TT 有什么建议呢?
  • lzh_coder:没有看到处理app进入前后台的逻辑,有时候定时器在进入后台的时候就停掉了
    Devil雅馨:@happyliuzh 恩恩 我在研究研究 :relaxed:
  • _YZG_:死循环啊
    Devil雅馨:@_YZG_ 不能啊 执行完之后就回去了
    _YZG_:@Devil雅馨 执行完之后
    Devil雅馨:@_YZG_ 没有呀 点击按钮才执行
  • 建筑工匠:我觉得一个按钮控件就可以了,不必要里面再套两个label。
    Devil雅馨:@木子歌 好的
  • 建筑工匠:我觉得一个按钮控件就可以了,不必要里面再套两个label。
    Devil雅馨:@木子歌 恩恩 我会按你的方法试试的
  • 雷晏:....说好的获取短信呢,怎么变成倒计时了。。。 :flushed:
    Devil雅馨:@雷晏 :grin: :grin: 弄明白会传上来的 别慌张
  • 405b454dbb8d:呵呵呵呵
    Devil雅馨:@悟空的裤衩 :flushed: :blush: :smiley: :smile: :grin: :stuck_out_tongue_winking_eye:
  • 蚂蚱Damon:说好的短信验证码呢? :sob: 你确定不是点击button,然后倒计时.
    Devil雅馨:@牧之Damon O(∩_∩)O哈哈~ 你真逗 :yum:
    蚂蚱Damon:@Devil雅馨 你要是去关注我,我就喜欢你,给你个赞
    Devil雅馨:@牧之Damon (*^__^*) 嘻嘻…… 等我搞懂了 给你发哈 :grin:
  • 鬼丶白:通过 ViewWithTag: 这种方法很耗效率
    VanChan:@Devil雅馨 用属性就不需要用tag这种方式了
    Devil雅馨:@soime O(∩_∩)O好的 那可以有点其他的建议么
  • f2736c363fc7:看不懂´_>`
    Devil雅馨:@我何时为仙orz 555 怎么会看不懂呢 特别简单的 :fist:

本文标题:点击获取短信验证码倒计时(iOS)

本文链接:https://www.haomeiwen.com/subject/asgssttx.html