gpt4 book ai didi

iphone - 在标签中显示计时器

转载 作者:行者123 更新时间:2023-12-03 18:31:24 25 4
gpt4 key购买 nike

与大多数游戏一样,我看到计时器的格式为“01:05”

我正在尝试实现一个计时器,重置时我需要将计时器重置为“00:00”。

这个计时器值应该在标签中。

如何创建一个递增的计时器?就像 00:00---00:01---00:02......类似 dat 的东西。

建议

问候

最佳答案

我使用的一个简单方法是:

//In Header
int timeSec = 0;
int timeMin = 0;
NSTimer *timer;

//Call This to Start timer, will tick every second
-(void) StartTimer
{
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerTick:) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
}

//Event called every time the NSTimer ticks.
- (void)timerTick:(NSTimer *)timer
{
timeSec++;
if (timeSec == 60)
{
timeSec = 0;
timeMin++;
}
//Format the string 00:00
NSString* timeNow = [NSString stringWithFormat:@"%02d:%02d", timeMin, timeSec];
//Display on your label
//[timeLabel setStringValue:timeNow];
timeLabel.text= timeNow;
}

//Call this to stop the timer event(could use as a 'Pause' or 'Reset')
- (void) StopTimer
{
[timer invalidate];
timeSec = 0;
timeMin = 0;
//Since we reset here, and timerTick won't update your label again, we need to refresh it again.
//Format the string in 00:00
NSString* timeNow = [NSString stringWithFormat:@"%02d:%02d", timeMin, timeSec];
//Display on your label
// [timeLabel setStringValue:timeNow];
timeLabel.text= timeNow;
}

关于iphone - 在标签中显示计时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3792597/

25 4 0