gpt4 book ai didi

iphone - 在 Objective-c 中实现倒数计时器?

转载 作者:IT王子 更新时间:2023-10-29 08:12:54 25 4
gpt4 key购买 nike

我是 iOS 编程新手。我正在研究单词匹配游戏。在这个游戏中,我需要实现显示分钟和秒的时间计数器。我希望当我的游戏开始时,我的计时器从 3 分钟开始。现在我想以秒为单位反向减少这个计时器。我的代码只工作了几秒钟..这是我的代码:

  secondsLeft--;

int seconds = (secondsLeft %3600) % 60;

Timerlbl.text = [NSString stringWithFormat:@"%02d",seconds];


if (seconds==0)

{


UIAlertView *pAlert = [[UIAlertView alloc]initWithTitle:@"Sorry!!"
message:@"TimeOver" delegate:self cancelButtonTitle:@"OK"
otherButtonTitles:@"Cancel",nil];

[pAlert show];

[pAlert release];

}

}

在 Viewdidload 中我通过计时器调用它..

          countDown=[NSTimer scheduledTimerWithTimeInterval:5.0 target:self 
selector:@selector(TimeOver) userInfo:nil repeats:YES];

请任何人指导我如何在几分钟和几秒钟内完成。

最佳答案

你可以这样做(启用 ARC):-

@interface ViewController()
{
UILabel *progress;
NSTimer *timer;
int currMinute;
int currSeconds;
}
@end


@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
progress=[[UILabel alloc] initWithFrame:CGRectMake(80, 15, 100, 50)];
progress.textColor=[UIColor redColor];
[progress setText:@"Time : 3:00"];
progress.backgroundColor=[UIColor clearColor];
[self.view addSubview:progress];
currMinute=3;
currSeconds=00;

// Do any additional setup after loading the view, typically from a nib.
}
-(void)start
{
timer=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerFired) userInfo:nil repeats:YES];

}
-(void)timerFired
{
if((currMinute>0 || currSeconds>=0) && currMinute>=0)
{
if(currSeconds==0)
{
currMinute-=1;
currSeconds=59;
}
else if(currSeconds>0)
{
currSeconds-=1;
}
if(currMinute>-1)
[progress setText:[NSString stringWithFormat:@"%@%d%@%02d",@"Time : ",currMinute,@":",currSeconds]];
}
else
{
[timer invalidate];
}
}

关于iphone - 在 Objective-c 中实现倒数计时器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10663184/

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