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

发布时间 - 2026-01-11 01:09:57    点击率:

方法一:

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

-(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  # 获取短信验证码  # 短信验证码倒计时  # 获取验证码倒计时  # IOS开发代码分享之用nstimer实现倒计时功能  # IOS实现验证码倒计时功能(一)  # IOS关于大型网站抢购、距活动结束  # 剩余时间倒计时的实现代码  # ios 实现倒计时的两种方式  # iOS中实现简单易懂秒杀倒计时/倒计时代码  # iOS中让多个cell上都出现倒计时的分析与实现  # iOS实现毫秒倒计时的方法详解  # iOS启动页倒计时跳过按钮功能  # Swift实现iOS应用中短信验证码倒计时功能的实例分享  # iOS实现秒杀活动倒计时  # 倒计时  # 直接调用  # 多说  # 验证码  # 大家多多  # 就可以  # 新建一个  # 网上  # forState  # beginAnimations  # setTitle  # UIControlStateNormal  # dispatch_async  # dispatch_source_cancel  # lt  # dispatch_source_set_event_handler  # getIdentifyCodeBt  # dispatch_get_main_queue  # whiteColor  # UIColor 


相关栏目: 【 网站优化151355 】 【 网络推广146373 】 【 网络技术251813 】 【 AI营销90571


相关推荐: Laravel Vite是做什么的_Laravel前端资源打包工具Vite配置与使用  如何确认建站备案号应放置的具体位置?  活动邀请函制作网站有哪些,活动邀请函文案?  标题:Vue + Vuex 项目中正确使用 JWT 进行身份认证的实践指南  魔方云NAT建站如何实现端口转发?  PHP的CURL方法curl_setopt()函数案例介绍(抓取网页,POST数据)  如何彻底删除建站之星生成的Banner?  ChatGPT回答中断怎么办 引导AI继续输出完整内容的方法  谷歌浏览器下载文件时中断怎么办 Google Chrome下载管理修复  香港服务器网站卡顿?如何解决网络延迟与负载问题?  Win11怎么设置默认图片查看器_Windows11照片应用关联设置  中国移动官方网站首页入口 中国移动官网网页登录  今日头条微视频如何找选题 今日头条微视频找选题技巧【指南】  Python文件流缓冲机制_IO性能解析【教程】  Laravel怎么导出Excel文件_Laravel Excel插件使用教程  iOS中将个别页面强制横屏其他页面竖屏  Laravel如何自定义分页视图?(Pagination示例)  想要更高端的建设网站,这些原则一定要坚持!  Laravel怎么做数据加密_Laravel内置Crypt门面的加密与解密功能  猪八戒网站制作视频,开发一个猪八戒网站,大约需要多少?或者自己请程序员,需要什么程序员,多少程序员能完成?  Laravel如何操作JSON类型的数据库字段?(Eloquent示例)  EditPlus中的正则表达式实战(6)  laravel怎么使用数据库工厂(Factory)生成带有关联模型的数据_laravel Factory生成关联数据方法  如何在IIS中新建站点并解决端口绑定冲突?  如何用狗爹虚拟主机快速搭建网站?  LinuxShell函数封装方法_脚本复用设计思路【教程】  如何用5美元大硬盘VPS安全高效搭建个人网站?  Laravel如何设置自定义的日志文件名_Laravel根据日期或用户ID生成动态日志【技巧】  广州网站制作公司哪家好一点,广州欧莱雅百库网络科技有限公司官网?  JavaScript中如何操作剪贴板_ClipboardAPI怎么用  Laravel如何记录自定义日志?(Log频道配置)  JS中页面与页面之间超链接跳转中文乱码问题的解决办法  Python图片处理进阶教程_Pillow滤镜与图像增强  Laravel用户认证怎么做_Laravel Breeze脚手架快速实现登录注册功能  如何获取上海专业网站定制建站电话?  Thinkphp 中 distinct 的用法解析  Laravel广播系统如何实现实时通信_Laravel Reverb与WebSockets实战教程  Laravel Livewire是什么_使用Laravel Livewire构建动态前端界面  Laravel的路由模型绑定怎么用_Laravel Route Model Binding简化控制器逻辑  Laravel如何实现全文搜索_Laravel Scout集成Algolia或Meilisearch教程  微博html5版本怎么弄发语音微博_语音录制入口及时长限制操作【教程】  香港服务器选型指南:免备案配置与高效建站方案解析  厦门模型网站设计制作公司,厦门航空飞机模型掉色怎么办?  Laravel如何实现用户角色和权限系统_Laravel角色权限管理机制  网站广告牌制作方法,街上的广告牌,横幅,用PS还是其他软件做的?  Swift中循环语句中的转移语句 break 和 continue  Java Adapter 适配器模式(类适配器,对象适配器)优缺点对比  Laravel如何升级到最新版本?(升级指南和步骤)  Laravel如何使用withoutEvents方法临时禁用模型事件  Laravel怎么实现微信登录_Laravel Socialite第三方登录集成