我如何在 xcode 4.5 中创建倒数计时器 [英] How do i create a countdown timer in xcode 4.5
问题描述
好的,我想创建多个计时器,它们都在不同的时间(25、50,1 分钟、1 分钟 30 秒...)开始,但我不知道如何让它在达到 0 和达到零时停止,将播放器"带到另一个视图.
Ok i want to create MULTIPLE timers that all start at a different time (25, 50,1 min, 1min 30s...) but i dont know how to make it stop when it reaches 0 and when it does reach zero, bring the "Player" to another view.
这是我的 .h 文件
@interface ViewController :UIViewController {
IBOutlet UILabel *seconds;
NSTimer *timer;
int MainInt;
}
@end
这是我的 .m 文件
@implementation ViewController
-(void)countDownDuration {
MainInt -= 1;
seconds.text = [NSString stringWithFormat:@"%i", MainInt];
}
-(IBAction)start:(id)sender {
MainInt = 25;
timer = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:@selector(countDownDuration)
userInfo:nil
repeats:YES];
}
@end
推荐答案
NSTimer 不会自动执行此操作,但将其添加到您的 countDownDuration 方法中很简单.例如:
NSTimer doesn't do this automatically, but it's trivial to add it to your countDownDuration method. For example:
-(void)countDownDuration {
MainInt -= 1;
seconds.text = [NSString stringWithFormat:@"%i", MainInt];
if (MainInt <= 0) {
[timer invalidate];
[self bringThePlayerToAnotherView];
}
}
当然要创建多个计时器.您可以将每个存储在不同的变量中,并为每个变量提供不同的选择器.但是如果你查看 NSTimer 的文档,回调方法实际上将计时器对象作为选择器;你忽略了它,但你不应该.
Of course you want to create multiple timers. You could store each one in a different variable and give each one a different selector. But if you look at the documentation for NSTimer, the callback method actually takes the timer object as a selector; you're ignoring it, but you shouldn't be.
同时,您可以将任何类型的对象存储为计时器的 userInfo,因此这是为每个计时器存储单独的当前倒计时值的好地方.
And meanwhile, you can store an object of any type you want as the userInfo for the timer, so that's a good place to stash a separate current countdown value for each timer.
所以,你可以这样做:
-(void)countDownDuration:(NSTimer *)timer {
int countdown = [[timer userInfo] reduceCountdown];
seconds.text = [NSString stringWithFormat:@"%i", countdown];
if (countdown <= 0) {
[timer invalidate];
[self bringThePlayerToAnotherView];
}
}
-(IBAction)start:(id)sender {
id userInfo = [[MyCountdownClass alloc] initWithCountdown:25];
timer = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:@selector(countDownDuration:)
userInfo:userInfo
repeats:YES];
}
我没有写一些细节(比如 MyCountdownClass
的定义——它必须包括方法 initWithCountdown:
和 reduceCountdown
正确的事情),但它们都应该非常简单.(另外,大概你想要一个 userInfo 来存储的不仅仅是倒计时值——例如,如果每个计时器将玩家发送到不同的视图,你也必须将视图存储在那里.)
I've left a few details unwritten (like the definition of MyCountdownClass
—which has to include method initWithCountdown:
and reduceCountdown
that do the right thing), but they should all be pretty simple. (Also, presumably you want a userInfo that stores more than just the countdown value—e.g., if each timer sends the player to a different view, you've got to stash the view there as well.)
PS,注意你现在需要@selector(countDownDuration:)
.ObjC 的新手总是把这搞砸.countDownDuration:
和 countDownDuration
是完全不相关的选择器.
PS, notice that you now need @selector(countDownDuration:)
. Newcomers to ObjC screw this up all the time. countDownDuration:
and countDownDuration
are completely unrelated selectors.
PPS,MyCountdownClass
的完整定义必须在 countDownDuration:
中可见(除非您有其他具有相同选择器的类).您可能希望将 userInfo
的结果显式转换为 MyCountdownClass *
以使事情更清楚.
PPS, the full definition of MyCountdownClass
will have to be visible in countDownDuration:
(unless you have some other class with the same selectors). You may want to explicitly cast the result of userInfo
to MyCountdownClass *
to make things clearer.
这篇关于我如何在 xcode 4.5 中创建倒数计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!