如何在 iOS 中从零开始计时
How to start a timer from zero in iOS
我有一个按钮,我在上面放置了一个 UITapGestureRecognizer。当我点击那个按钮时,我调用了一个开始计时的方法。
我的问题是,我能够在标签上获取计时器字符串。但是计时器从当前日期时间开始,我想总是从零开始计时器(就像倒数计时器)。下面是我的定时器代码。
-(IBAction)micPressed{
if (gestureRecognizer.state==UIGestureRecognizerStateBegan) {
gestureRecognizer.view.alpha=0.2f;
[label setHidden:NO];
if (!_timer) {
_timer = [NSTimer scheduledTimerWithTimeInterval:0.1f
target:self
selector:@selector(_timerFired:)
userInfo:nil
repeats:YES];
}
}
else{
[label setHidden:YES];
gestureRecognizer.view.alpha=10.2f;
if ([_timer isValid]) {
[_timer invalidate];
}
_timer = nil;
}
}
- (void)_timerFired:(NSTimer *)timer {
NSDateFormatter *dateformatter=[[NSDateFormatter alloc]init];
[dateformatter setDateFormat:@"mm:ss:SSS"];
NSString *dateInStringFormated=[dateformatter stringFromDate:[NSDate dateWithTimeIntervalSinceNow:0]];
NSLog(@"%@",dateInStringFormated);
[label setText:dateInStringFormated];
}
谁能给我建议解决方案。感谢任何帮助。
问题是您是从当前时间开始计时的。制作一个类似的倒数计时器。您需要执行以下操作:
- 取一个名为
Counter
的 int
并从 0
初始化它
- 触发你的
NSTimer
,然后增加计数器
- 在
UILabel
文本中显示计数器值。
- 一旦您的
NSTimer
停止,再次到达 0
。
就是这样!这就是您可以达到预期结果的方法。
Code:
int counter = 0;
- (void)_timerFired:(NSTimer *)timer {
counter++;
}
一旦NSTimer
再次失效,使其变为counter = 0
你的代码应该是这样的,
NSDateFormatter *dateformatter=[[NSDateFormatter alloc]init];
[dateformatter setDateFormat:@"mm:ss:SSS"];
NSString *dateInStringFormated=[dateformatter stringFromDate:[dateformatter dateFromString:@"00:00:000"]];
NSLog(@"test : %@",dateInStringFormated);
我刚把 [NSDate dateWithTimeIntervalSinceNow:0]
改成了 [dateformatter dateFromString:@"00:00:000"]
。
它总是从零开始。
更新:
我想你想要更新分钟、秒和毫秒的计时器之类的东西,因为我现在理解你的问题。你可以实现它,
像这样安排计时器,
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(updateTimer) userInfo:nil repeats:YES];
现在你的 updateTimer
应该是这样的,
-(void)updateTimer{
static double counter = 0;
static int counter2 = 0;
int seconds = (int)counter % 60;
int minutes = ((int)counter / 60) % 60;
NSLog(@"%02d:%02d:%03d",minutes,seconds,counter2); // This should be your labels text that updating continuously
counter = counter + 0.01;
counter2++;
if (counter2 == 100) {
counter2 = 0;
}
}
希望这会有所帮助:)
记住你启动定时器的时间:
_startDate = [NSDate date];
_Timer = ...
然后在您的 _timerFired
方法中使用自开始日期以来的时间间隔:
NSDateComponentsFormatter *formatter = [[NSDateComponentsFormatter alloc] init];
NSString *dateInStringFormatted = [formatter stringFromTimeInterval:[[NSDate date] timeIntervalSinceDate:_startDate]];
我有一个按钮,我在上面放置了一个 UITapGestureRecognizer。当我点击那个按钮时,我调用了一个开始计时的方法。 我的问题是,我能够在标签上获取计时器字符串。但是计时器从当前日期时间开始,我想总是从零开始计时器(就像倒数计时器)。下面是我的定时器代码。
-(IBAction)micPressed{
if (gestureRecognizer.state==UIGestureRecognizerStateBegan) {
gestureRecognizer.view.alpha=0.2f;
[label setHidden:NO];
if (!_timer) {
_timer = [NSTimer scheduledTimerWithTimeInterval:0.1f
target:self
selector:@selector(_timerFired:)
userInfo:nil
repeats:YES];
}
}
else{
[label setHidden:YES];
gestureRecognizer.view.alpha=10.2f;
if ([_timer isValid]) {
[_timer invalidate];
}
_timer = nil;
}
}
- (void)_timerFired:(NSTimer *)timer {
NSDateFormatter *dateformatter=[[NSDateFormatter alloc]init];
[dateformatter setDateFormat:@"mm:ss:SSS"];
NSString *dateInStringFormated=[dateformatter stringFromDate:[NSDate dateWithTimeIntervalSinceNow:0]];
NSLog(@"%@",dateInStringFormated);
[label setText:dateInStringFormated];
}
谁能给我建议解决方案。感谢任何帮助。
问题是您是从当前时间开始计时的。制作一个类似的倒数计时器。您需要执行以下操作:
- 取一个名为
Counter
的int
并从0
初始化它
- 触发你的
NSTimer
,然后增加计数器 - 在
UILabel
文本中显示计数器值。 - 一旦您的
NSTimer
停止,再次到达0
。
就是这样!这就是您可以达到预期结果的方法。
Code:
int counter = 0;
- (void)_timerFired:(NSTimer *)timer {
counter++;
}
一旦NSTimer
再次失效,使其变为counter = 0
你的代码应该是这样的,
NSDateFormatter *dateformatter=[[NSDateFormatter alloc]init];
[dateformatter setDateFormat:@"mm:ss:SSS"];
NSString *dateInStringFormated=[dateformatter stringFromDate:[dateformatter dateFromString:@"00:00:000"]];
NSLog(@"test : %@",dateInStringFormated);
我刚把 [NSDate dateWithTimeIntervalSinceNow:0]
改成了 [dateformatter dateFromString:@"00:00:000"]
。
它总是从零开始。
更新:
我想你想要更新分钟、秒和毫秒的计时器之类的东西,因为我现在理解你的问题。你可以实现它,
像这样安排计时器,
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(updateTimer) userInfo:nil repeats:YES];
现在你的 updateTimer
应该是这样的,
-(void)updateTimer{
static double counter = 0;
static int counter2 = 0;
int seconds = (int)counter % 60;
int minutes = ((int)counter / 60) % 60;
NSLog(@"%02d:%02d:%03d",minutes,seconds,counter2); // This should be your labels text that updating continuously
counter = counter + 0.01;
counter2++;
if (counter2 == 100) {
counter2 = 0;
}
}
希望这会有所帮助:)
记住你启动定时器的时间:
_startDate = [NSDate date];
_Timer = ...
然后在您的 _timerFired
方法中使用自开始日期以来的时间间隔:
NSDateComponentsFormatter *formatter = [[NSDateComponentsFormatter alloc] init];
NSString *dateInStringFormatted = [formatter stringFromTimeInterval:[[NSDate date] timeIntervalSinceDate:_startDate]];