NSTimer 详细设置1:http://blog.csdn.net/davidsph/article/details/7899483

NSTimer 详细设置2:http://blog.csdn.net/davidsph/article/details/7899731
1 、、、、、

先说一下我的业务需求,最近在做一个小项目,需要用到定时器的功能,NSTimer类,期间,出现了一些小问题,不过最终通过自己的努力,终于做出来了。我想总结一下,我对NSTimer类的学习和理解。

不多说了,先上效果图

界面元素很简单,两个UIButton 开始和暂停,20表示起始倒计时。最终的效果是,按开始按钮的时候,倒计时开始运行,按暂停按钮的时候,计时器,停止倒计时。当倒计时为0的时候,弹出一个对话框,提示时间已到。

业务需求很简单,但是,在我的实现中,却出现了,一些小错误。 主要是暂停键不能点击多次,开始键也不能点击多次,我相信,刚开始,接触这个NSTimer的人,也会出现这几个问题。

直接上几个主要的代码:

控制器类的.h文件中

@interface sdsViewController : UIViewController<UIAlertViewDelegate>

//定义一个定时器,做为实例变量
@property(nonatomic,retain) NSTimer *timer;

//显示倒计时当前状态
@property (retain, nonatomic) IBOutlet UILabel *timeDisplay;

//开始按钮,响应的action
- (IBAction)startTime:(id)sender;

//暂停按钮响应的action
- (IBAction)stopTime:(id)sender;

@end

.m中关键代码

开始按钮 响应代码:

- (IBAction)startTime:(id)sender {

//如果定时器对象不存在,则创建一个并启动
    
    if(!_timer){
        
        //创建一个定时器,这个是直接加到当前消息循环中,注意与其他初始化方法的区别
       _timer=[NSTimer
scheduledTimerWithTimeInterval:1.0 target:self
selector:@selector(changeTimeAtTimedisplay) userInfo:nil repeats:YES];

//  [_timer fire]; //对于这个fire方法,稍后会详解,它不是启动一个定时器,这么简单
        
    }

}

//结束按钮响应代码:

- (IBAction)stopTime:(id)sender {
    
    if (_timer) {
        NSLog(@"调用 self.time为真!!");

//如果定时器在运行
        if ([self.timer isValid]) {

NSLog(@"单击停止按钮,取消定时器!!");
        
            [self.timer invalidate];

//这行代码很关键
           _timer=nil;
            
        }
        
    }
    
}

一切OK,现在分析程序用到的关键地方。

先看看NSTimer类的结构,比较简单

Tasks

Creating a Timer

//创建一个定时器 ,以下是便利构造器方法,都懂的
+ scheduledTimerWithTimeInterval:invocation:repeats:
+ scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:
+ timerWithTimeInterval:invocation:repeats:
+ timerWithTimeInterval:target:selector:userInfo:repeats:

//初始化方法
– initWithFireDate:interval:target:selector:userInfo:repeats:

//是开始一个定时器吗,恐怕没那么简单????????
Firing a Timer
– fire

//是暂停一个定时器吗,NO ,是Stop ,写的很清楚

Stopping a Timer
– invalidate

//关于定时器的以下信息
Information About a Timer
– isValid  //是否在运行
– fireDate //Returns the date at which the receiver will fire.
– setFireDate: //重新设置定时器开始运行的时间
– timeInterval  //定时器延时时间
– userInfo //其他信息

------------------------------------------------------
2、、、、、、、、、

先说一下,初始化方法

+ scheduledTimerWithTimeInterval:invocation:repeats:
+ scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:

这两个是创建一个定时器,并加入到当前运行循环中,即我们可以这样去理解,这样初始化一个定时器时,在(NSTimeInterval)seconds 时间之后,自动启动定时器。

而以下两个初始化方法这不一样:

+ timerWithTimeInterval:invocation:repeats:
+ timerWithTimeInterval:target:selector:userInfo:repeats:


两个同样是创建,但没有加入到,运行循环中。class method to create the timer object without
scheduling it on a run loop.然后,建立之后,必须(after creating it, you must add
the timer to a run loop manually by
calling the addTimer:forMode: method of the corresponding NSRunLoop
object.),这就是与上面两个方法的区别。

//创建一个定时器
       _timer=[NSTimer scheduledTimerWithTimeInterval:1.0 target:self
selector:@selector(changeTimeAtTimedisplay) userInfo:nil repeats:YES];

_timer=[NSTimer timerWithTimeInterval:10 target:self selector:@selector(changeTimeAtTimedisplay) userInfo:nil repeats:YES];
       //必须手动加入到当前循环中去
       NSRunLoop *runloop=[NSRunLoop currentRunLoop];
       [runloop addTimer:_timer forMode:NSDefaultRunLoopMode];

以上两端代码效果是一样的

关于这个方法:

Firing a Timer
– fire

其实他并不是真的启动一个定时器,从之前的初始化方法中我们也可以看到,建立的时候,在适当的时间,定时器就会自动启动。那这个方法是干什么的呢。

You
can use this method to fire a repeating timer without interrupting its
regular firing
schedule. If the timer is non-repeating, it is automatically
invalidated after firing, even if its scheduled fire date has not
arrived.

这是官方文档的说法,英文说的很清楚,但我们理解还不是很到位,为了彻底搞懂它的功能。我又做了一个测试。

也是很简单,把上面那个定时器,改变一点

//初始化的时候创建一个定时器

- (id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    
    if (self) {
        // Custom initialization
        
        //创建一个定时器,
        
       _timer=[NSTimer timerWithTimeInterval:10 target:self
selector:@selector(changeTimeAtTimedisplay) userInfo:nil repeats:YES];

//手动加入到循环中

NSRunLoop *runloop=[NSRunLoop currentRunLoop];

[runloop addTimer:_timer forMode:NSDefaultRunLoopMode];

//当然这个定时器会自动启动,只不多过了十秒之后,才触发

}

return self

}

当我们单击“开始”按钮时,

- (IBAction)startTime:(id)sender {
   
    //只是简单地调用一下这个方法,看到底功能是什么
  [_timer fire];

}


果是,单击一下按钮,倒计时减1,单击一下减1,即它把触发的时间给提前了,但过十秒后倒计时还会减1,即它只是提前触发定时器,而不影响之前的那个定时
器设置的时间,就好比我们等不及要去看一场球赛,赶紧把车开快些一样,fire的功能就像让我们快些到球场,但却不影响球赛开始的时间。


记得之前那个初始化定时器时,设置的是YES吗,当我们,改为NO时,即不让它循环触发时,我们此时再单击开始按钮。会猛然发现,倒计时减1了,但当我们
再点击开始按钮时,会发现倒计时,不会动了。原因是:我们的定时器,被设置成只触发一次,再fire的时候,触发一次,该定时器,就被自动销毁了,以后再
fire也不会触发了。

现在 我们再看官方的解释,或许就会更明白了,

You
can use this method to fire a repeating timer without interrupting its
regular firing
schedule. If the timer is non-repeating, it is automatically
invalidated after firing, even if its scheduled fire date has not
arrived.

这就对了,fire并不是启动一个定时器,只是提前触发而已。

NSTimer 详细设置的更多相关文章

  1. jfreeChart柱状图各属性详细设置

    一. 下载与环境配置 此最新版本为 1.0.13 解压jfreechart-1.0.13.zip 将lib目录下的jfreechart-1.0.13.jar .jcommon-1.0.16.jar 复 ...

  2. Win2008远程多用户登陆的配置方法 另附详细设置: Windows server 2008 R2实现多用户远程连接

    Win2008远程多用户登陆的配置方法     在使用Windows 2008远程登录功能时,如果需要进行多用户登录,可以采用以下配置方法: 首先要启用远程桌面这一功能:右击“我的电脑”→ 属性 → ...

  3. FlexSlider插件的详细设置参数 http://www.woothemes.com/flexslider/

    http://www.woothemes.com/flexslider/ FlexSlider插件的详细设置参数 $(window).load(function() { $('.flexslider' ...

  4. c# webConfig中的session超时详细设置

    webConfig中的session超时详细设置 我们在webConfig中设置Session超时的时候,如果最后发行的地址是远程服务器,我们很多不是必须的属性并不用设置,如果设之后,倒不能让 ses ...

  5. Eclipse详细设置护眼背景色和字体颜色并导出

    Eclipse详细设置护眼背景色和字体颜色并导出 Eclipse是一款码农们喜闻乐见的集成开发平台,但是其默认的主题和惨白的背景色实在是太刺激眼球了.下面,将给大家详细介绍如何设置成护眼主题的方法,也 ...

  6. Log4j详细设置说明

    1. 动态的改变记录级别和策略,即修改log4j.properties,不需要重启Web应用,这需要在web.xml中设置一下.2. 把log文件定在 /WEB-INF/logs/ 而不需要写绝对路径 ...

  7. [Eclipse] 详细设置护眼背景色和字体颜色并导出

    http://jingyan.baidu.com/article/d5a880eb6c4f7813f147ccef.html Eclipse是一款码农们喜闻乐见的集成开发平台,但是其默认的主题和惨白的 ...

  8. windows server 2003 负载平衡的详细设置步骤(转载)

    声明:本文为转载. 当把一台服务器(包括Web服务器.FTP服务器或者流媒体服务器等等)放入网络中之后,随着客户端数量的不断增加,人们往往需要功能更强大.处理速度更快的服务器.为了解决这个问题,如果将 ...

  9. WIN8共享文件 详细设置

    原文地址:http://jingyan.baidu.com/article/75ab0bcbff4274d6864db2f5.html win8共享文件设置 详细教程 | 浏览:6987 | 更新:2 ...

随机推荐

  1. 【转】Linux中安装Resin

      安装步骤: Ø  安装resin前先要保证安装了JDK,可以用命令查看是否安装了JDK: [root@wxr webapps]# java -versions java version " ...

  2. DFBle.swift

    ////  DFBle.swift//  DFBle////  Created by LeeYaping on 15/9/2.//  Copyright (c) 2015年 lisper. All r ...

  3. Android 自定义控件玩转字体变色 打造炫酷ViewPager指示器

    1.概述 本篇博客的产生呢,是因为,群里的哥们暖暖给我发了个效果图,然后问我该如何实现顶部ViewPager指示器的字体变色,该效果图是这样的: 大概是今天头条的app,神奇的地方就在于,切换View ...

  4. C#自定义List类

    代码如下: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespac ...

  5. Jquery JSOPN在WebApi中的问题

    1. 客户端代码: $.ajax({ data: { name: 'zhangsan' }, url: apiUrl.getTwo('TestFourth'), dataType: 'jsonp', ...

  6. FalseEasting 和 FalseNorthing

    FalseEasting (东伪偏移) 和FalseNorthing(北伪偏移) FalseEasting (东伪偏移) 投影平面中为避免横轴(经度方向)坐标出现负值,而所加的偏移量.我国规定将高斯- ...

  7. Swift中的协议

    协议: 1.Swift协议用于定义多个类型应该遵守的规范 2.协议定义了一种规范, 不提供任何实现 3.协议统一了属性名, 方法, 下标, 但是协议并不提供任何实现 4.语法格式: [修饰符] pro ...

  8. LAMP 搭建wordpress部署教程贴.

    LAMP 搭建wordpress部署教程贴.这是一篇主要将LAMP,并且通过wordpress来进行验证,演示.如何去部署PHP CMS很多新手看到LAMP就很很头大,觉得很难搞,编译安装,搞了好几天 ...

  9. JavaScript 高级程序设计 第5章引用类型 笔记

    第五章 引用类型 一.object类型 1.创建方法: 1.使用new 操作符创建 var person=new object() Person.name=”Nicholasa” Porson.age ...

  10. 有n人围成一圈,顺序排号。从第1个人开始报数(从1到3报数),凡报到3的人退出圈子,问最后留下的是原来的第几号的那位。

    #include <iostream> using namespace std; int main() { int i,j,n,m,k,*p,num[100];k=m=0;   cin&g ...