scheduledTimerWithTimeInterval与timerWithTimeInterval的区别

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:

//初始化方法
&ndash; initWithFireDate:interval:target:selector:userInfo:repeats:

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

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

Stopping a Timer
&ndash; invalidate

//关于定时器的以下信息
Information About a Timer
&ndash; isValid //是否在运行
&ndash; fireDate //Returns the date at which the receiver will fire.
&ndash; setFireDate: //重新设置定时器开始运行的时间
&ndash; timeInterval //定时器延时时间
&ndash; 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
&ndash; 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];

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用\[1\]: \[runLoop addTimer:myTimer forMode:NSDefaultRunLoopMode\]; //实际上这步是不需要,scheduledTimerWithTimeInterval已经纳入当前线程运行。如果使用timerWithTimeInterval则需要。 引用\[2\]: \[\[NSRunLoop currentRunLoop\] addTimer:_timer forMode:NSDefaultRunLoopMode\]; //_timer = \[NSTimer scheduledTimerWithTimeInterval:1.f target:self selector:@selector(timerAction) userInfo:nil repeats:YES\]; 引用\[3\]: GCD的定时器不受RunLoop中Mode的影响(RunLoop内部也是基于GCD实现的,可以根据源码看到), 比如滚动TableView的时候,GCD的定时器不受影响;且比NSTimer更加准时。 问题: NSTimer的mode是什么意思? 回答: NSTimer的mode是指定定时器在运行时所处的运行循环模式。在使用NSTimer时,可以通过指定mode来控制定时器在哪些运行循环模式下运行。比如在引用\[1\]和引用\[2\]中,都使用了NSDefaultRunLoopMode作为定时器的运行循环模式。这意味着定时器会在默认的运行循环模式下运行。而GCD的定时器则不受RunLoop中Mode的影响,可以在任何运行循环模式下运行,如引用\[3\]所示。 #### 引用[.reference_title] - *1* [iOS多线程的初步研究(四)-- NSTimer](https://blog.csdn.net/lengshengren/article/details/12905635)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [NSTimer 基本使用和注意事项](https://blog.csdn.net/wutengwei007/article/details/82221069)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值