gpt4 book ai didi

ios - 创建多个 NSTimers 倒计时

转载 作者:行者123 更新时间:2023-11-29 10:42:54 26 4
gpt4 key购买 nike

我正在尝试为自己创建一个简单的倒计时应用程序。到目前为止,我已经想出如何创建倒数计时器,并为我附加到计时器的单个按钮对它们执行停止/重置操作。

但是,我想将多个计时器添加到同一个页面,但我不确定如何对计时器进行额外调用。每个计时器都有自己的倒计时数字(一个为 7 分钟,另一个为 3 分钟,等等)。这些是用户无法更改的设置间隔。谷歌并没有真正为我解决如何做到这一点,所以我希望有人至少可以指导我朝着正确的方向前进。以下是我的代码片段:

ViewController.h

@interface ViewController : UIViewController {
IBOutlet UILabel *firstCountdownLabel;
NSTimer *firstCountdownTimer;
bool timerActive;
int secondsCount;
}

- (IBAction)start:(id)sender;
- (void)timerRun;

@end

ViewController.m

@interface ViewController ()

@end

@implementation ViewController

- (void) timerRun {
secondsCount = secondsCount - 1;
int minutes = secondsCount / 60;
int seconds = secondsCount - (minutes * 60);

NSString *timerOutput = [NSString stringWithFormat:@"%2d:%.2d", minutes, seconds];
firstCountdownLabel.text = timerOutput;

if (secondsCount == 0) {
[firstCountdownTimer invalidate];
firstCountdownTimer = nil;
}
}

//- (void) setTimer {
- (IBAction)start:(id)sender {
secondsCount = 420;
if (timerActive == NO) {
timerActive = YES;
self->firstCountdownTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerRun) userInfo:nil repeats:YES];
}
else {
timerActive=NO;
[self->firstCountdownTimer invalidate];
self->firstCountdownTimer = nil;
}
}

- (void)viewDidLoad
{
[super viewDidLoad];
// [self setTimer];
// Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end

最佳答案

Google 不会帮助您展示如何实现原创应用创意。

如果你想要多个定时器,定义多个定时器实例变量:

@interface ViewController : UIViewController 
{
IBOutlet UILabel *timer1Label;
IBOutlet UILabel *timer2Label;
IBOutlet UILabel *timer3Label;
NSTimer *timer1;
NSTimer *timer2;
NSTimer *timer3;
int timer1Count;
int timer2Count;
int timer3Count;
bool timer1Active;
bool timer2Active;
bool timer3Active;
}

然后为启动每个计时器的每个按钮创建一个单独的 IBAction:

- (IBAction)startTimer1:(id)sender 
{
timer1Count = 420;
if (timer1Active == NO)
{
timer1Active = YES;
timer1 = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:@selector(timer1Run:)
userInfo:nil
repeats:YES];
}
else
{
timer1Active=NO;
[timer1 invalidate];
timer1 = nil;
}
}

- (void) timer1Run: (NSTimer*) timer
{
timer1Count -= 1;
int minutes = timer1Count / 60;
int seconds = timer1Count - (minutes * 60);

NSString *timerOutput = [NSString stringWithFormat:@"%2d:%.2d", minutes, seconds];
timer1Label = timerOutput;

if (timer1Count == 0) {
[timer2 invalidate];
timer2 = nil;
}
}

为每个计时器复制上述代码,使用“timer2”和“timer3”代替“timer1”。将每一个的时间计数更改为所需的值。 (我将名称从“firstTimer”更改为“timer1”,因为这样更容易编辑代码以支持多个计时器。

我没有为您编写每个方法的 3 个版本,因为您需要弄清楚这一点,而不是复制和粘贴您不理解的代码。

有可能,并且需要更少的代码,对所有启动计时器按钮使用相同的 IBAction 方法,并让代码检查按钮上的标签以决定启动哪个计时器。

代码可能是这样的:

- (IBAction)startTimer1:(id)sender 
{
int tag = [sender tag];
switch (tag)
{
case 1: //timer 1
//Put code to start timer 1 here
break;
case 2: //timer 2
//put code to start timer 2 here
break;
}
}

但此刻您可能有点难以理解。

顺便说一下,忘记你曾经见过“self->variable”语法吧。它比直接引用实例变量更慢且更容易出错。使用 object->variable 语法还允许您访问其他对象的实例变量,这是不好的做法。您应该始终使用属性来访问您以外的对象的实例变量。

此外,定时器方法应该采用单个参数,即定时器。我在上面的代码中更正了计时器方法。

关于ios - 创建多个 NSTimers 倒计时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23631978/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com