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

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

详细实现方式是使用了iOS 自带的 NSTimer

上代码

首先新建

    int secondsCountDown; //倒计时总时长
NSTimer *countDownTimer;
UILabel *labelText;

然后详细实现

    //创建UILabel 加入到当前view
labelText=[[UILabel alloc]initWithFrame:CGRectMake(10, 120, 120, 36)];
[self.view addSubview:labelText]; //设置倒计时总时长
secondsCountDown = 60;//60秒倒计时
//開始倒计时
countDownTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeFireMethod) userInfo:nil repeats:YES]; //启动倒计时后会每秒钟调用一次方法 timeFireMethod //设置倒计时显示的时间
labelText.text=[NSString stringWithFormat:@"%d",secondsCountDown];

实现每秒钟运行的方法

-(void)timeFireMethod{
//倒计时-1
secondsCountDown--;
//改动倒计时标签现实内容
labelText.text=[NSString stringWithFormat:@"%d",secondsCountDown];
//当倒计时到0时。做须要的操作,比方验证码过期不能提交
if(secondsCountDown==0){
[countDownTimer invalidate];
[labelText removeFromSuperview];
}
}

大致已经实现,有问题可继续交流

苹果开发群 :414319235  欢迎增加  欢迎讨论问题

iOS 倒计时NSTimer的更多相关文章

  1. iOS定时器-- NSTimer 和CADisplaylink

    iOS定时器-- NSTimer 和CADisplaylink 一.iOS中有两种不同的定时器: 1.  NSTimer(时间间隔可以任意设定,最小0.1ms)// If seconds is les ...

  2. iOS - OC NSTimer 定时器

    前言 @interface NSTimer : NSObject 作用 在指定的时间执行指定的任务. 每隔一段时间执行指定的任务. 1.定时器的创建 当定时器创建完(不用 scheduled 的,添加 ...

  3. iOS之 NSTimer(一)

    以前没怎么了解过这个NSTimer,其实还是有挺多坑的,今天来总结一下: 首先我们一起来看这个: 我在A  -> (push) -> B控制器,然后再B控制器中开启了一个NSTimer.然 ...

  4. iOS 定时器 NSTimer、CADisplayLink、GCD3种方式的实现

    在软件开发过程中,我们常常需要在某个时间后执行某个方法,或者是按照某个周期一直执行某个方法.在这个时候,我们就需要用到定时器. 然而,在iOS中有很多方法完成以上的任务,到底有多少种方法呢?经过查阅资 ...

  5. 小程序实现倒计时:解决ios倒计时失效(setInterval失效)

    在使用之前需要先在page页引入wxTimer.js文件(这里我将文件放在/utils) let timer = require('../../utils/wxTimer.js'); 然后就可以使用啦 ...

  6. iOS 里面 NSTimer 防止 循环引用

    使用NSTimer的类 #import "TBTimerTestObject.h" #import "TBWeakTimerTarget.h" @interfa ...

  7. iOS - Swift NSTimer 定时器

    前言 public class NSTimer : NSObject 作用 在指定的时间执行指定的任务. 每隔一段时间执行指定的任务. 1.定时器的创建 当定时器创建完(不用 scheduled 的, ...

  8. iOS定时器NSTimer的使用方法

    1.初始化 + (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelect ...

  9. 【转】IOS 计时器 NSTimer

    原文网址:http://blog.csdn.net/tangshoulin/article/details/7644124 1.初始化 + (NSTimer *)timerWithTimeInterv ...

随机推荐

  1. kNN的维数灾难与PCA降维

    主成分分析 PCA 协方差矩阵 假设我们有 \[ X = \begin{pmatrix}X_1\\X_2\\\vdots\\X_m\end{pmatrix}\in\mathbb{R}^{m\times ...

  2. 聊聊、Java 网络编程

    Socket 编程大家都不陌生,Java 学习中必学的部分,也是 Java网络编程核心内容之一.Java 网络编程又包括 TCP.UDP,URL 等模块.TCP 对应 Socket模块,UDP 对应  ...

  3. 深度理解STL之map、set

    课程设计做了这个一直没有整理(搬运 set算是关键字和相同的特殊map set应该更加被强调理解为“集合”,而集合所涉及的操作并.交.差等,即STL提供的如交集set_intersection().并 ...

  4. 使用MySQLWorkBench连接数据库

    在日常使用中,经常使用命令行去连接MySQL数据库不方便,推荐使用MySQLWorkBench去远程连接进行管理,下面记录一下使用步骤: 1. 安装MySQLWorkBench后,界面如下,点击“+” ...

  5. git status 下中文显示乱码问题解决

      $ git status -s                 ?? "\350\257\264\346\230\216.txt\n                 $ printf & ...

  6. php hash防止表单

    <?php /** * Created by PhpStorm. * User: brady * Desc: * Date: 2017/7/12 * Time: 15:01 */class te ...

  7. java EL详解

    转自:http://www.codeceo.com/article/java-el-usage.html 一.EL简介 1.语法结构 ${expression} 2.[]与.运算符 EL 提供.和[] ...

  8. HDU——1027Ignatius and the Princess II(next_permutation函数)

    Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K ( ...

  9. input和textarea区别

    1. input是单行文本,textarea是多行文本,可以带滚动条2. input的value放在标签里面 <input type="text" value="b ...

  10. js对象的扁平化与反扁平化

    Object.flatten = function(obj){ var result = {}; function recurse(src, prop) { var toString = Object ...