iOS获取短信验证码倒计时的两种实现方法

所属分类: 软件编程 / IOS 阅读数: 1479
收藏 0 赞 0 分享

方法一:

网上用的很多的一种,不多说,直接上代码.

-(void)startTime{
  __block int timeout= 60; //倒计时时间
  dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
  dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
  dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
  dispatch_source_set_event_handler(_timer, ^{
    if(timeout<=0){ //倒计时结束,关闭
      dispatch_source_cancel(_timer);
      dispatch_async(dispatch_get_main_queue(), ^{
        [self.getIdentifyCodeBt setTitle:@"获取验证码" forState:UIControlStateNormal];
        self.getIdentifyCodeBt.userInteractionEnabled = YES;
        [self.getIdentifyCodeBt setTitleColor:THEME_RED forState:UIControlStateNormal];
        self.getIdentifyCodeBt.backgroundColor = [UIColor whiteColor];
        self.getIdentifyCodeBt.layer.borderColor = THEME_RED.CGColor;
      });
    }else{
      dispatch_async(dispatch_get_main_queue(), ^{

        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:1];
        [self.getIdentifyCodeBt setTitle:[NSString stringWithFormat:@"%zd秒后失效",timeout] forState:UIControlStateNormal];
        [self.getIdentifyCodeBt setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        self.getIdentifyCodeBt.backgroundColor = [UIColor lightGrayColor];
        self.getIdentifyCodeBt.layer.borderColor = [UIColor clearColor].CGColor;
        self.getIdentifyCodeBt.clipsToBounds = YES;
        [UIView commitAnimations];
        self.getIdentifyCodeBt.userInteractionEnabled = NO;
      });
      timeout--;
    }
  });
  dispatch_resume(_timer);

}

到时直接调用就可以了。

方法二:利用分类

给UIButton新建一个分类

.h文件如下

#import <UIKit/UIKit.h>

@interface UIButton (XSCountDown)
- (void)xs_beginCountDownWithDuration:(NSTimeInterval)duration;
- (void)xs_stopCountDown;
@end

.m文件如下

#import "UIButton+XSCountDown.h"

#import "ThemeColor.h"
static NSTimer *_countTimer;
static NSTimeInterval _count;
static NSString *_title;

@implementation UIButton (XSCountDown)

- (void)xs_beginCountDownWithDuration:(NSTimeInterval)duration {
  _title = self.titleLabel.text;
  _count = duration;
  _countTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(xs_updateTitle) userInfo:nil repeats:YES];
  [[NSRunLoop mainRunLoop] addTimer:_countTimer forMode:NSRunLoopCommonModes];
  self.userInteractionEnabled = NO;

   [self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  self.backgroundColor = [UIColor lightGrayColor];
  self.layer.borderColor = [UIColor clearColor].CGColor;
  self.clipsToBounds = YES;
}

- (void)xs_stopCountDown {
  [_countTimer invalidate];
  _countTimer = nil;
  _count = 60.0;
  [self setTitle:_title forState:UIControlStateNormal];
  self.userInteractionEnabled = YES;
}

- (void)xs_updateTitle {
  NSString *countString = [NSString stringWithFormat:@"%lis 后失效", (long)_count - 1];
  self.userInteractionEnabled = NO;
  [self setTitle:countString forState:UIControlStateNormal];
  if (_count-- <= 1.0) {
    [self xs_stopCountDown];
    [self setTitleColor:THEME_RED forState:UIControlStateNormal];
    self.backgroundColor = [UIColor whiteColor];
    self.layer.borderColor = THEME_RED.CGColor;
  }

}

@end

然后在controller里直接调用分类.h文件里的方法就ok了

[self.verifyBt xs_beginCountDownWithDuration:60.0];

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

详细整理iOS中UITableView的性能优化

最近在微博上看到一个很好的开源项目,是关于如何优化UITableView的,加上正好最近也在优化项目中的类似朋友圈功能这块,思考了很多关于UITableView的优化技巧,所以决定详细的整理下对优化UITableView的理解,需要的朋友们可以参考借鉴。
收藏 0 赞 0 分享

IOS开发中禁止NavigationController的向右滑动返回

这篇文章主要介绍了IOS开发中禁止NavigationController的向右滑动返回的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

iOS实现微信/QQ显示最近拍摄图片的功能实例代码

如果你刚刚拍摄了图片,在使用微信/QQ发生消息时会显示“你可能要发送的图片”,这个功能非常人性化,怎么实现的呢?下面小编给大家分享iOS实现微信/QQ显示最近拍摄图片的功能实例代码,一起看看吧
收藏 0 赞 0 分享

iOS实现动态自适应标签

这篇文章主要为大家详细介绍了iOS动态自适应标签的相关资料,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

iOS实现图片存在本地、再从本地获取图片的功能

本文主要介绍了iOS实现图片存在本地、再从本地获取图片的功能的代码。具有很好的参考价值。下面跟着小编一起来看下吧
收藏 0 赞 0 分享

iOS视频录制(或选择)压缩及上传功能(整理)

最新做的一个功能涉及到了视频的录制、压缩及上传功能,经过大神的一番教导,终于倒腾清楚了,今天小编把问题经过记录一下分享到脚本之家平台,供大家参考
收藏 0 赞 0 分享

iOS中打包上传常见的错误与解决办法

关于打包上传至AppStore,大家都认为是最后一步了,其实到了这里往往会遇到很多的坑。对于踩过的坑我不想再踩第二遍,所以在此将我遇到的所有奇葩问题在此做一个记录,当作对自己的一个提醒,同时也分享给给位,需要的朋友可以参考下。
收藏 0 赞 0 分享

解决Xcode 8构建版本iTunes Connect获取不到应用程序状态的办法

这篇文章主要介绍了关于解决Xcode 8构建版本iTunes Connect获取不到应用程序状态的办法,需要的朋友可以参考下
收藏 0 赞 0 分享

Objective-C实现身份证验证的方法示例

这篇文章主要给大家分享了Objective-C实现身份证验证的方法,文中给出了详细的示例代码,对大家具有一定的参考价值,需要的朋友们下面来一起看看吧。
收藏 0 赞 0 分享

ios启动页强制竖屏(进入App后允许横屏与竖屏)

最近工作遇到这样一个需要,当进入启动页需要强制竖屏,而进入APP后就允许横屏与竖屏,通过查找相关的资料找到了解决的方法,所以将实现的方法整理后分享出来,需要的朋友们可以参考借鉴,下面来一起看看吧。
收藏 0 赞 0 分享
查看更多