xcode (NSTimer) 创建一个计时器倒计时
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15311289/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
(NSTimer) creating a Timer Countdown
提问by Matthew Nieuwenhuys
I am trying to create a simple Countdown Timer so that when a player enters my game the timer begins from 60 down to 0. It seems simple but I am getting confused at how I write this.
我正在尝试创建一个简单的倒数计时器,以便当玩家进入我的游戏时,计时器从 60 开始到 0。这看起来很简单,但我对如何编写此内容感到困惑。
So far I have created a method within my GameController.m that looks like this:
到目前为止,我已经在我的 GameController.m 中创建了一个如下所示的方法:
-(int)countDownTimer:(NSTimer *)timer {
[NSTimer scheduledTimerWithTimeInterval:-1
invocation:NULL
repeats:YES];
reduceCountdown = -1;
int countdown = [[timer userInfo] reduceCountdown];
if (countdown <= 0) {
[timer invalidate];
}
return time;
}
At the start of the game I initialise the integer Time at 60. The label is then being set within ViewController. But at the moment when I compile the code it just shows the label at 60 and doesn't decrement at all.
在游戏开始时,我将整数 Time 初始化为 60。然后在 ViewController 中设置标签。但是在我编译代码的那一刻,它只是在 60 处显示标签,并且根本没有减少。
Any help would be greatly appreciated - I am new to Objective-C.
任何帮助将不胜感激 - 我是 Objective-C 的新手。
EDIT
编辑
With some assistance from I have now separated the code into 2 separate methods. The code now looks like this:
在我的帮助下,我现在已将代码分成 2 个单独的方法。代码现在看起来像这样:
-(void)countDown:(NSTimer *)timer {
if (--time == 0) {
[timer invalidate];
NSLog(@"It's working!!!");
}
}
-(void)countDownTimer:(NSTimer *)timer {
NSLog(@"Hello");
[NSTimer scheduledTimerWithTimeInterval:1
target:self
selector:@selector(countDown:)
userInfo:nil
repeats:YES];
}
HOWEVER, the code is still not running properly and when I call the method [game countDownTimer] from my View Controller it breaks saying: "unrecognized selector sent to instance". Can anybody explain what is wrong here?
但是,代码仍然无法正常运行,当我从我的视图控制器调用方法 [game countDownTimer] 时,它会中断说:“无法识别的选择器发送到实例”。任何人都可以解释这里有什么问题吗?
回答by dasblinkenlight
Several things are wrong with your code:
您的代码有几处错误:
- You are passing a wrong parameter for the time interval- negative numbers are interpreted as the 0.1 ms
- You are calling the wrong overload- you are expected to pass an invocation object, yet you are passing a
NULL
- You put the code that you want executed on timer together with timer initialization- the code that needs to be executed on timer should go into a separate method.
- 您为时间间隔传递了错误的参数- 负数被解释为 0.1 ms
- 您正在调用错误的重载- 您应该传递一个调用对象,但您传递的是一个
NULL
- 您将要在计时器上执行的代码与计时器初始化放在一起- 需要在计时器上执行的代码应该放在一个单独的方法中。
You should call the overload that takes a selector, and pass 1
for the interval, rather than -1
.
您应该调用带有选择器的重载,并传递1
间隔,而不是-1
.
Declare NSTimer *timer
and int remainingCounts
, then add
声明NSTimer *timer
and int remainingCounts
,然后添加
timer = [NSTimer scheduledTimerWithTimeInterval:1
target:self
selector:@selector(countDown)
userInfo:nil
repeats:YES];
remainingCounts = 60;
to the place where you want to start the countdown. Then add the countDown method itself:
到要开始倒计时的地方。然后添加 countDown 方法本身:
-(void)countDown {
if (--remainingCounts == 0) {
[timer invalidate];
}
}
回答by bbarnhart
Try this
尝试这个
- (void)startCountdown
{
_counter = 60;
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1
target:self
selector:@selector(countdownTimer:)
userInfo:nil
repeats:YES];
}
- (void)countdownTimer:(NSTimer *)timer
{
_counter--;
if (_counter <= 0) {
[timer invalidate];
// Here the counter is 0 and you can take call another method to take action
[self handleCountdownFinished];
}
}
回答by Tirupathi Raju
From the question you posed.you can achieve that by invoking a function for every 1 sec and handle the decrement logic in that.
从您提出的问题来看,您可以通过每 1 秒调用一个函数并处理其中的递减逻辑来实现这一点。
Snippet:-
片段:-
NSTimer *t = [NSTimer scheduledTimerWithTimeInterval: 1.0
target: self
selector:@selector(onTick:)
userInfo: nil repeats:YES];
(void)onTick
{
//do what ever you want
NSLog(@"i am called for every 1 sec");
//invalidate after 60 sec [timer invalidate];
}