Ios 暂停计时器/秒表

Ios 暂停计时器/秒表,ios,objective-c,nstimer,Ios,Objective C,Nstimer,在项目中,我的工作,我需要有一个秒表,将暂停和继续。到目前为止,所有的基本功能都能正常工作,但我还没有找到暂停计时器并重新启动它的方法。仅供参考,我已经检查了其他帖子,但它们不起作用。代码: .h: 在您的情况下,您正在使用记录按钮最初按下的NSDate计算秒表标签的值。无法以这种方式暂停计时器,因为每次重新计算秒表标签的值时,它都会反映按下记录按钮的原始日期。我建议将此方法更改为以下内容: - (void)updateTimer { // Timer is 1/10 of a s

在项目中,我的工作,我需要有一个秒表,将暂停和继续。到目前为止,所有的基本功能都能正常工作,但我还没有找到暂停计时器并重新启动它的方法。仅供参考,我已经检查了其他帖子,但它们不起作用。代码:

.h:


在您的情况下,您正在使用记录按钮最初按下的
NSDate
计算秒表标签的值。无法以这种方式暂停计时器,因为每次重新计算秒表标签的值时,它都会反映按下记录按钮的原始日期。我建议将此方法更改为以下内容:

 - (void)updateTimer
 {
     // Timer is 1/10 of a second so thats what we add to stopwatch
     NSTimeInterval timeInterval = 0.1;

    // Create a date formatter
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"HH:mm:ss.SSS"];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]];

    // Take the time currently displayed on the stopwatch and add the time interval to it
    NSDate *oldDate = [dateFormatter dateFromString:self.stopwatchLabel.text];
    NSDate *newDate = [oldDate dateByAddingTimeInterval:timeInterval];
    //Get a string representation of the new date and BOOM POW.
    NSString *timeString = [dateFormatter stringFromDate:newDate];
    self.stopwatchLabel.text = timeString;
 }

我还没有测试过这个,但我希望它能工作。如果还有一些语法问题,我也不会感到惊讶。另外,请确保
self.stopwatchLabel.text中的字符串符合开始格式(例如00:00:00.000)。

尝试在
-(void)AudioPlayerDifinishPlaying:(AVAudioPlayer*)player中注释代码


我的猜测是,在
-(iAction)RecordPauseTaped:(id)sender
中,您正在调用
[player stop]
,这将触发
-(void)AudioPlayerDifinishPlaying:(AVAudioPlayer*)player成功:(BOOL)标志
,使您的新计时器无效。

当我现在单击暂停计时器时,它仍会继续运行。现在它不像以前那样启动计时器了。我把[自动秒表计时器失效];self.stopWatchTimer=nil;[自更新程序];在暂停录制注释下。您可以有多个计时器吗?使计时器无效应该会使其停止。然后要恢复计时,你应该制作一个新的计时器。我在哪里恢复秒表?如果(!recorder.recording){“相关”部分显示的几个结果具体出了什么问题?不要只说“它们不工作”,否则我会将你的问题标记为重复。
- (IBAction)recordPauseTapped:(id)sender {

     if ([stopwatchLabel.text  isEqual: @"00:00:00.000"]) {


     self.startDate = [NSDate date];

     // Create the stop watch timer that fires every 100 ms
     self.stopWatchTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/10.0
                                                       target:self
                                                     selector:@selector(updateTimer)
                                                     userInfo:nil
                                                      repeats:YES];



     // Stop the audio player before recording
     if (player.playing) {
     [player stop];
    }

if (!recorder.recording) {
    AVAudioSession *session = [AVAudioSession sharedInstance];
    [session setActive:YES error:nil];

    // Start recording
    [recorder record];
    [recordPauseButton setTitle:@"Pause" forState:UIControlStateNormal];

}
}else {

    // Pause recording
    [self.stopWatchTimer invalidate];
    self.stopWatchTimer = nil;
    [self updateTimer];
    [recorder pause];
    [recordPauseButton setTitle:@"Record" forState:UIControlStateNormal];

}

[stopButton setEnabled:YES];
[playButton setEnabled:NO];


}
 - (void)updateTimer
 {
     // Timer is 1/10 of a second so thats what we add to stopwatch
     NSTimeInterval timeInterval = 0.1;

    // Create a date formatter
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"HH:mm:ss.SSS"];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]];

    // Take the time currently displayed on the stopwatch and add the time interval to it
    NSDate *oldDate = [dateFormatter dateFromString:self.stopwatchLabel.text];
    NSDate *newDate = [oldDate dateByAddingTimeInterval:timeInterval];
    //Get a string representation of the new date and BOOM POW.
    NSString *timeString = [dateFormatter stringFromDate:newDate];
    self.stopwatchLabel.text = timeString;
 }
- (IBAction)recordPauseTapped:(id)sender {

     if ([stopwatchLabel.text  isEqual: @"00:00:00.000"]) {


     self.startDate = [NSDate date];

     // Create the stop watch timer that fires every 100 ms
     self.stopWatchTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/10.0
                                                       target:self
                                                     selector:@selector(updateTimer)
                                                     userInfo:nil
                                                      repeats:YES];



     // Stop the audio player before recording
     if (player.playing) {
     [player stop];
    }

if (!recorder.recording) {
    AVAudioSession *session = [AVAudioSession sharedInstance];
    [session setActive:YES error:nil];

    // Start recording
    [recorder record];
    [recordPauseButton setTitle:@"Pause" forState:UIControlStateNormal];

}
}else {

    // Pause recording
    [self.stopWatchTimer invalidate];
    self.stopWatchTimer = nil;
    [self updateTimer];
    [recorder pause];
    [recordPauseButton setTitle:@"Record" forState:UIControlStateNormal];

}

[stopButton setEnabled:YES];
[playButton setEnabled:NO];


}