NSTimer 怎么暂停继续

来源:互联网 发布:淘宝网女士羊毛衣 编辑:程序博客网 时间:2024/05/16 09:41

转载自:http://blog.csdn.net/chentoo/article/details/8667918

那,API里面NSTimer 是木有暂停继续的方法的,只有fire和invalidate,前者是开工的意思,后者是废掉的意思,如果用废掉来代替暂停的功能?显然是不对的。

那肿么办呢?
其实NSTimer 有一个属性叫 fireDate ,啥意思呢?fireDate么,就是fire 的开始时间所以我们就有了思路了。

暂停: [timer setFireDate:[NSDate distantFuture]]; distantFuture,就是问你未来有多远呢?好远好远就是无法到达的时间,所以 timer就一直等待不 fire了。也就是暂停了。

继续: [timer setFireDate:[NSDate date]]; 这个当然就是把fire 的时间设置为当前时刻,所以timer就立刻开工啦!

完事!


------------------------------------------------------------------------------

http://www.cnblogs.com/crazyac/articles/2497477.html
NSTimer的暂停和继续

可以用NSTimer设置访问函数的时间周期:

复制代码

countTime = [NSTimerscheduledTimerWithTimeInterval:(1.0/60.0

                                     target:self 

                                   selector:@selector(updateBoard) 

                                   userInfo:nil 

                                    repeats:YES];

 

 

复制代码

即每1/60秒调用一次updateBoard函数

但是有时候游戏里要暂停,可以这样:

[countTime setFireDate:[NSDate distantFuture]]; 

需要继续的时候

[countTime setFireDate:[NSDate date]]; 
0 0
原创粉丝点击