objective-c - 使用 NSTimer 的秒表错误地在显示中包含暂停时间

标签 objective-c ios cocoa-touch time nstimer

这是我的 iPhone 秒表代码。它按预期工作,并在单击按钮时停止和恢复。

但是,当我点击“停止”时,计时器不会停止在后台运行,而当我点击“开始”恢复它时,它会更新时间并跳到当前位置而不是从那里恢复停止的时间。

如何停止 NSTimer?发生这种情况的原因是什么?

@implementation FirstViewController;
@synthesize stopWatchLabel;

NSDate *startDate;
NSTimer *stopWatchTimer;
int touchCount;


-(void)showActivity {

    NSDate *currentDate = [NSDate date];
    NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:startDate];
    NSDate *timerDate = [NSDate dateWithTimeIntervalSince1970:timeInterval];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"mm:ss.SS"];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]];
    NSString *timeString=[dateFormatter stringFromDate:timerDate];
    stopWatchLabel.text = timeString;
    [dateFormatter release];
}

- (IBAction)onStartPressed:(id)sender {

    stopWatchTimer = [NSTimer scheduledTimerWithTimeInterval:1/10 target:self selector:@selector(showActivity) userInfo:nil repeats:YES];

    touchCount += 1;
    if (touchCount > 1)
    {
        [stopWatchTimer fire];
    }
    else 
    {
        startDate = [[NSDate date]retain];
        [stopWatchTimer fire];

    }
}

- (IBAction)onStopPressed:(id)sender {
    [stopWatchTimer invalidate];
    stopWatchTimer = nil;
    [self showActivity];
}

- (IBAction)reset:(id)sender; {
    touchCount = 0;
    stopWatchLabel.text = @"00:00.00";
}

最佳答案

您对当前显示的计算始终使用计时器的原始开始时间,因此暂停后的显示包括计时器暂停的时间间隔。

最简单的做法是存储另一个 NSTimeInterval,比如 secondsAlreadyRun,当计时器暂停时,并将其添加到您恢复时计算的时间间隔.每次计时器开始计时时,您都需要更新计时器的 startDate 。在 reset: 中,您还将清除 secondsAlreadyRun 间隔。

-(void)showActivity:(NSTimer *)tim {

    NSDate *currentDate = [NSDate date];
    NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:startDate];
    // Add the saved interval
    timeInterval += secondsAlreadyRun;
    NSDate *timerDate = [NSDate dateWithTimeIntervalSince1970:timeInterval];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"mm:ss.SS"];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]];
    NSString *timeString=[dateFormatter stringFromDate:timerDate];
    stopWatchLabel.text = timeString;
    [dateFormatter release];
}

- (IBAction)onStartPressed:(id)sender {

    stopWatchTimer = [NSTimer scheduledTimerWithTimeInterval:1/10 
                                                      target:self 
                                                    selector:@selector(showActivity:) 
                                                    userInfo:nil 
                                                     repeats:YES];
    // Save the new start date every time
    startDate = [[NSDate alloc] init]; // equivalent to [[NSDate date] retain];
    [stopWatchTimer fire];
}

- (IBAction)onStopPressed:(id)sender {
    // _Increment_ secondsAlreadyRun to allow for multiple pauses and restarts
    secondsAlreadyRun += [[NSDate date] timeIntervalSinceDate:startDate];
    [stopWatchTimer invalidate];
    stopWatchTimer = nil;
    [startDate release];
    [self showActivity];
}

- (IBAction)reset:(id)sender; {
    secondsAlreadyRun = 0;
    stopWatchLabel.text = @"00:00.00";
}

不要忘记在适当的地方发布 startDate!另请记住,记录的 NSTimer 接口(interface)用于您为其提供的方法接受一个参数,该参数将是计时器本身。没有它似乎也行得通,但为什么要试探命运呢?

最后,由于您经常使用 NSDateFormatter,您可能需要考虑将其设为 ivar 或将其放入 showActivity 的 static 存储中: ,像这样:

static NSDateFormatter * dateFormatter = nil;
if( !dateFormatter ){
    dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"mm:ss.SS"];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]];
}

关于objective-c - 使用 NSTimer 的秒表错误地在显示中包含暂停时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8101231/

相关文章:

objective-c - 用手指触摸移动时防止 View 对象跳跃

objective-c - 保留 kPasteboardTypeFileURLPromise 时的错误处理

objective-c - 查找两地之间的路线数

iphone - 如何在 tableView 单元格中圆化 ImageView 的角

android - 从年龄中找到 DateOFBirth

javascript - 在cocoa webview中调用网页的Javascript函数

ios - 应用程序在选项卡栏之间的快速导航之间崩溃

objective-c - [NSObject 描述]

ios - 在服务器上发送图像数组?

ios - UITextView 中的属性文本导致崩溃