倒计时的实现(转)

项目中可能会遇到有些倒计时的地方

比如 手机验证的时候,验证码一般都会有一个时间限制,此时在输入验证码的地方就需要展示一个倒计时


使用了iOS 自带的 NSTimer

[objc] view plain copy
  1. int secondsCountDown; //倒计时总时长  
  2. NSTimer *countDownTimer;  
  3. UILabel *labelText;  


然后具体实现


[objc] view plain copy
  1. //创建UILabel 添加到当前view  
  2. labelText=[[UILabel alloc]initWithFrame:CGRectMake(1012012036)];  
  3. [self.view addSubview:labelText];  
  4.   
  5. //设置倒计时总时长  
  6. secondsCountDown = 60;//60秒倒计时  
  7. //开始倒计时  
  8. countDownTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeFireMethod) userInfo:nil repeats:YES]; //启动倒计时后会每秒钟调用一次方法 timeFireMethod  
  9.   
  10. //设置倒计时显示的时间  
  11. labelText.text=[NSString stringWithFormat:@"%d",secondsCountDown];  

实现每秒钟执行的方法
[objc] view plain copy
  1. -(void)timeFireMethod{  
  2.     //倒计时-1  
  3.     secondsCountDown--;  
  4.     //修改倒计时标签现实内容  
  5.     labelText.text=[NSString stringWithFormat:@"%d",secondsCountDown];  
  6.     //当倒计时到0时,做需要的操作,比如验证码过期不能提交  
  7.     if(secondsCountDown==0){  
  8.         [countDownTimer invalidate];  
  9.         [labelText removeFromSuperview];  
  10.     }  

转载于:https://www.cnblogs.com/tommychang/p/5433399.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值