【问题标题】:Stopwatch issue in XcodeXcode 中的秒表问题
【发布时间】:2012-02-28 17:17:00
【问题描述】:

您好,我的应用中有秒表。

我的秒表有一个开始、停止和重置按钮。

停止和重置按钮起作用

开始有点像。

当用户第一次点击开始按钮时,它会启动秒表。如果他们点击停止按钮,然后再点击开始按钮,秒表就会重新启动。

我缺少什么(下面列出的代码)?

.h

    IBOutlet UILabel *stopWatchLabel;
    NSTimer *stopWatchTimer; // Store the timer that fires after a certain time
    NSDate *startDate; // Stores the date of the click on the start button
    @property (nonatomic, retain) IBOutlet UILabel *stopWatchLabel;
    - (IBAction)onStartPressed;
    - (IBAction)onStopPressed;
    - (IBAction)onResetPressed;
    - (void)updateTimer

.m

    - (void)updateTimer{

    NSDate *currentDate = [NSDate date];
    NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:startDate];
    NSDate *timerDate = [NSDate dateWithTimeIntervalSince1970:timeInterval];

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"HH:mm:ss:SSS"];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]];
    NSString *timeString=[dateFormatter stringFromDate:timerDate];
    stopWatchLabel.text = timeString;

    }

    - (IBAction)onStartPressed {
    startDate = [NSDate date];

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

    - (IBAction)onStopPressed {
    [stopWatchTimer invalidate];
    stopWatchTimer = nil;
    [self updateTimer]; 
    }

    - (IBAction)onResetPressed {
    stopWatchLabel.text = @"00:00:00:000";
    }

任何帮助都会很棒。

干杯

【问题讨论】:

    标签: xcode4.2 nstimer stopwatch


    【解决方案1】:

    在上面的代码中,您正在存储启动-停止操作期间经过的时间。为此,您需要在班级级别添加NSTimeInterval totalTimeInterval 变量。最初或按下重置按钮时,其值将设置为 0。在updateTimer 方法中,您需要替换以下代码。

    NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:startDate];
    totalTimeInterval += timeInterval;
    NSDate *timerDate = [NSDate dateWithTimeIntervalSince1970:totalTimeInterval ];
    

    谢谢,

    【讨论】:

      【解决方案2】:

      timeInterval 会增加,比如 1,2,3,4,5 等

      totalTimeInterval = totalTimeInterval + timeInterval -> 0 = 0 + 1 
      

      下次我们调用 updateTimer 函数时,totalTimeInterval 将是 1 = 1 + 2,所以 totalTimeInterval 将是 3。

      所以如果我们显示 totalTimeInterval,秒数将为 1 , 3 , 6 , ....etc。

      1. 首先,您需要在类级别中使用 NSTimeInterval totalTimeInterval 和 NSTimeInterval timeInterval 变量

      2. updateTimer 方法,您需要替换以下代码。

        timeInterval = [currentDate timeIntervalSinceDate:startDate];
        timeInterval += totalTimeInterval;
        NSDate *timerDate = [NSDate dateWithTimeIntervalSince1970:timeInterval];
        
      3. 然后是 onStopPressed 方法和以下代码。

        totalTimeInterval = timeInterval;
        

      谢谢。

      【讨论】:

        猜你喜欢
        • 2011-08-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-10-30
        • 2015-12-14
        • 2011-08-15
        • 1970-01-01
        • 2018-04-15
        相关资源
        最近更新 更多