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

 更新时间:2017年05月18日 11:00:42   作者:APP叫我取个帅气的昵称  
本篇文章主要介绍了iOS获取短信验证码倒计时的两种实现方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

方法一:

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

-(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导航栏对控制器view的影响详解

    iOS导航栏对控制器view的影响详解

    这篇文章主要给大家介绍了关于iOS导航栏对控制器view的影响的相关资料,文中通过示例代码介绍的非常详细,对各位iOS开发者们具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-09-09
  • 基于iOS Realm数据库的使用实例详解

    基于iOS Realm数据库的使用实例详解

    下面小编就为大家分享一篇基于iOS Realm数据库的使用实例详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-01-01
  • iOS动画教你编写Slack的Loading动画进阶篇

    iOS动画教你编写Slack的Loading动画进阶篇

    这篇文章主要为大家进一步详细介绍了iOS动画教你编写Slack的Loading动画,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-09-09
  • iOS面试中如何优雅回答Block导致循环引用的问题

    iOS面试中如何优雅回答Block导致循环引用的问题

    这篇文章主要给大家介绍了iOS面试中关于如何优雅回答Block导致循环引用的问题的相关资料,文中通过图文介绍的非常相信,相信对大家具有一定的参考价值,需要的朋友们下面来一起看看吧。
    2017-03-03
  • iOS中视频播放器的简单封装详解

    iOS中视频播放器的简单封装详解

    要实现封装视频播放器,首先需要实现视频播放器,然后再去考虑怎样封装可以让以后自己使用起来方便快捷。iOS9之前可以使用MediaPlayer来进行视频的播放,iOS9之后系统推荐使用AVFoundation框架实现视频的播放。下面通过本文来看看详细的介绍吧。
    2016-10-10
  • iOS10 适配-Xcode8问题总结及解决方案

    iOS10 适配-Xcode8问题总结及解决方案

    这篇文章主要介绍了iOS10 适配-Xcode8问题总结的相关资料,这里整理了遇到的几种问题,并给出解决方案,需要的朋友可以参考下
    2016-11-11
  • IOS开发基础之二维数组详解

    IOS开发基础之二维数组详解

    这篇文章主要介绍了IOS开发基础之二维数组详解的相关资料,需要的朋友可以参考下
    2017-04-04
  • iOS 验证码按钮倒计时功能

    iOS 验证码按钮倒计时功能

    在app注册或者登录需要验证码的地方、为了避免短时间内刷验证码、往往会加上一层验证当倒计时结束后、可以重新获取,关于ios 验证码按钮倒计时功能大家可以参考下本文
    2017-07-07
  • iOS消息发送和转发示例详解

    iOS消息发送和转发示例详解

    这篇文章主要给大家介绍了关于iOS消息发送和转发的相关资料,用Objective-C的术语来讲,这叫做“给某个对象发送某条消息”。文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。
    2018-03-03
  • iOS左滑手势失效的解决方法

    iOS左滑手势失效的解决方法

    这篇文章主要为大家详细介绍了iOS左滑手势失效的解决方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-09-09

最新评论