NSTimer

关于NSTimer,最近在使用的时候,发现一些有意思的东西,和大家分享一下,其实也满简单的,平时只要多注意一些就能够很好得使用它们。比如我们定义NSTimer的实例的时候,一般有两种方式:

#1:

NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:selfselector:@selector(changeTimeAtTimedisplay) userInfo:nil repeats:YES];

[timer fire];


#2:

NSTimer *timer = [NSTimer timerWithTimeInterval:5.0 target:selfselector:@selector(changeTimeAtTimedisplay) userInfo:nil repeats:YES];

[[NSRunLoop currentRunLoopaddTimer:timer forMode:NSDefaultRunLoopMode];


以上两种方式的区别在于第二种方式定义后需要手动得将NSTimer加入到当前NSRunLoop的循环中去,这个在timerWithTimeInterval的定义中有如下的说明:

“You must add the new timer to a run loop, using addTimer:forMode:. Then, after seconds seconds have elapsed, the timer fires, sending the message aSelector to target. (If the timer is configured to repeat, there is no need to subsequently re-add the timer to the run loop.)Parameters”


特别需要说明的是,如果你定义一个NSTimer,那么如果你没有调用 fire 方法的话,那它会在一个timer interval之后调用它的selector方法,开始它的schedule,否则的话,它会立刻调用selector方法。对于repeat为YES的情况,fire方法只会提前NSTimer的执行,而对于repeat为NO的情况,则也会立刻执行,但是只执行一遍,第二遍不会再执行了。
"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."
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值