我搜索了所有主题,但没有一个答案是我真正清楚的。所以,我想创建一个 nstimer,它将在几分钟内从用户那里检索整数,并以秒为单位开始倒计时。如果用户放置 15 个计时器,我希望它以 14:59、14:58 的格式开始计时。我执行以下代码几秒钟。我的意思是用户以秒为单位给出一个整数并启动计数器。
setTime -= 1;
minutes = setTime /60;
seconds = setTime - (minutes *60);
NSString * timeroutput = [NSString stringWithFormat:@"%2d:%.2d", minutes, seconds];
所以我尝试了以下代码,但我无法在几秒钟内倒计时。有什么建议吗?
minutes = setTime;
seconds = (setTime * 60) % 60;
NSString * timeroutput = [NSString stringWithFormat:@"%2d:%.2d", minutes, seconds];
我的计时器设置有以下代码:
-(void)setTimer{
Timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerRun) userInfo:nil repeats:YES];
}
在 (timerRun) 方法中我实现了时间。
最佳答案
NSInteger seconds;
NSInteger userMinutes;
NSTimer *myTimer;
-(void)setTimer
{
seconds = userMinutes*60;
myTimer = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:@selector(timerRun)
userInfo:nil
repeats:YES];
}
-(void)timerRun
{
if (seconds-- > 0)
NSString *timerOutput = [NSString stringWithFormat:@"%02ld:%02ld", seconds/60, seconds%60];
else
[myTimer invalidate];
}
关于ios - Ntimer 实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25098720/