如何在目标 C 中重置计时器? [英] How to reset a timer in Objective C?
本文介绍了如何在目标 C 中重置计时器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在这个应用程序中创建了一个计数器,当我点击一个按钮时它开始并在我点击同一个按钮时停止.
I create a counter in this app that it starts when I click a button and stops when I click at the same button.
问题是,当我再次开始时,显示计数的标签不会重置,而是从停止的地方开始计数.
The problem is that when I start again, the label that displays the count does not reset it keeps the counting from where it stopped.
所以有人可以帮助我吗??
So any one could help me??
- (void)timerTick:(NSTimer *)timer {
_lblCounter.text = @"0:00:00";
double seconds = 0.0;
double minutes = 0.0;
double hours = 0.0;
ticks += 1.0;
seconds = fmod(ticks, 60.0);
minutes = fmod(trunc(ticks / 60.0), 60.0);
hours = trunc(ticks / 3600.0);
_lblCounter.text = [NSString stringWithFormat:@"%2.0f:%02.0f:%02.0f", hours, minutes, seconds];
}
- (IBAction)btnProcessa:(UIButton *)sender {
if ([sender.titleLabel.text isEqualToString:@"Iniciar"]) {
//Zera contador
_lblCounter.text = @"0:00:00";
//Define hora de início
_startTime = [NSDate date];
//Exibe hora inicial formatada no Label
NSString *strHoraInicial = [[self formatter] stringFromDate:_startTime];
_lblStartTime.text = strHoraInicial;
//Contador
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerTick:) userInfo:nil repeats:YES];
//Modifica botão
[sender setTitle:@"Encerrar" forState:UIControlStateNormal];
[sender setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
NSLog(@"Hora de início: %@", strHoraInicial);
} else {
//Para contador
[timer invalidate];
//Define a hora do encerramento
NSDate *currentTime = [NSDate date];
//Exibe hora final formatada no Label
NSString *strHoraFinal = [[self formatter] stringFromDate:currentTime];
_lblEndTime.text = strHoraFinal;
//Calcula a diferença e exibe no Label
NSTimeInterval elapsedTime = [currentTime timeIntervalSinceDate:_startTime];
double cost = elapsedTime / 3600.0 * 5.0;
NSString *valor = [[NSString alloc] initWithFormat:@"R$ %.2f",cost];
NSLog(@"%@", valor);
_lblValorPagar.text = valor;
float fltQtdPessoa = [_txtQtdPessoa.text floatValue];
float fltValor = cost / fltQtdPessoa;
NSString *strValor = [[NSString alloc] initWithFormat:@"R$ %.2f", fltValor];
_lblValorPessoa.text = strValor;
//Modifica Botão
[sender setTitle:@"Iniciar" forState:UIControlStateNormal];
[sender setTitleColor:[UIColor colorWithRed:0.208 green:0.710 blue:0.271 alpha:1.000] forState:UIControlStateNormal];
}
}
推荐答案
嘿伙计们,我设法通过这样的编码解决了这个问题:
Hey guys I managed to fix the problem by coding like this:
- (void)timerTick:(NSTimer *)timer {
NSDate *strStartTime = timer.userInfo;
NSTimeInterval interval = -[strStartTime timeIntervalSinceNow];
NSLog(@"%.0f", interval);
div_t h = div(interval, 3600);
float hours = h.quot;
div_t m = div(h.rem, 60);
float minutes = m.quot;
float seconds = m.rem;
_lblCounter.text = [[NSString alloc] initWithFormat:@"%2.0f:%02.0f:%02.0f", hours, minutes, seconds];
}
- (IBAction)btnProcessa:(UIButton *)sender {
if ([sender.titleLabel.text isEqualToString:@"Iniciar"]) {
//Zera contador
_lblCounter.text = @"0:00:00";
//Define hora de início
_startTime = [NSDate date];
//Exibe hora inicial formatada no Label
NSString *strHoraInicial = [[self formatter] stringFromDate:_startTime];
_lblStartTime.text = strHoraInicial;
//Contador
contador = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerTick:) userInfo:[NSDate date] repeats:YES];
//Modifica botão
[sender setTitle:@"Encerrar" forState:UIControlStateNormal];
[sender setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
NSLog(@"Hora de início: %@", strHoraInicial);
} else {
//Para contador
[contador invalidate];
//Define a hora do encerramento
NSDate *currentTime = [NSDate date];
//Exibe hora final formatada no Label
NSString *strHoraFinal = [[self formatter] stringFromDate:currentTime];
_lblEndTime.text = strHoraFinal;
//Calcula a diferença e exibe no Label
NSTimeInterval elapsedTime = [currentTime timeIntervalSinceDate:_startTime];
double cost = elapsedTime / 3600.0 * 5.0;
NSString *valor = [[NSString alloc] initWithFormat:@"R$ %.2f",cost];
NSLog(@"%@", valor);
_lblValorPagar.text = valor;
float fltQtdPessoa = [_txtQtdPessoa.text floatValue];
float fltValor = cost / fltQtdPessoa;
NSString *strValor = [[NSString alloc] initWithFormat:@"R$ %.2f", fltValor];
_lblValorPessoa.text = strValor;
//Modifica Botão
[sender setTitle:@"Iniciar" forState:UIControlStateNormal];
[sender setTitleColor:[UIColor colorWithRed:0.208 green:0.710 blue:0.271 alpha:1.000] forState:UIControlStateNormal];
}
}
这篇关于如何在目标 C 中重置计时器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文