iPhone秒表 - 开始、停止和重新开始却不能继续上次的计时

3
我是一名有用的助手,可以为您翻译文本。
我正在尝试制作一个秒表应用程序,并且在使其正常工作方面遇到了问题。当我启动秒表并停止并重新启动时,它不会从停止的位置继续运行。它继续运行,好像没有停止过。需要一些指导来使其正常工作。我一直在尝试自早上以来,其他功能与苹果的秒表类似,只有这个问题困扰着我... 非常感谢任何帮助...
代码如下:
ViewController.h
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>{
    UILabel *lbl;
    NSTimer *stopTimer;
    NSDate *startDate,*currentDate;
    BOOL running;
    UIButton *bttn;
    NSMutableArray *tableItems;
    NSString *timeString,*currentString;
    UITableView *tableview;
    int counter;
}

@property (strong,nonatomic) IBOutlet UILabel *lbl;
@property (strong,nonatomic) IBOutlet UIButton *bttn;
@property (strong,nonatomic) NSMutableArray *tableItems;
@property (strong,nonatomic) NSString *timeString;
@property (strong,nonatomic) IBOutlet UITableView *tableview;

-(IBAction)startPressed:(id)sender;
-(IBAction)resetPressed:(id)sender;

-(void)updateTimer;

@end

ViewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize lbl,bttn,tableItems,timeString,tableview;

- (void)viewDidLoad
{
    [super viewDidLoad];
    lbl.text = @"00.00.00.0";
    running = FALSE;
    //difference = @"0";
    startDate = [NSDate date];
    tableItems = [[NSMutableArray alloc] init];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(IBAction)startPressed:(id)sender{
    if(!running){
        running = TRUE;
        [sender setTitle:@"Stop" forState:UIControlStateNormal];
        [bttn setTitle:@"Lap" forState:UIControlStateNormal];
        if (stopTimer == nil) {
            stopTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/10.0
                                                         target:self
                                                       selector:@selector(updateTimer)
                                                       userInfo:nil
                                                        repeats:YES];
        }
    }else{
        running = FALSE;
        //startDate = currentDate;
        //difference = currentString;
        [sender setTitle:@"Start" forState:UIControlStateNormal];
        [bttn setTitle:@"Restart" forState:UIControlStateNormal];
        [stopTimer invalidate];
        stopTimer = nil;
    }

}
-(void)updateTimer{
    currentDate = [NSDate date];
    NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:startDate];
    NSDate *timerDate = [NSDate dateWithTimeIntervalSince1970:timeInterval];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"HH:mm:ss.S"];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]];
    timeString=[dateFormatter stringFromDate:timerDate];
    lbl.text = timeString;
}

-(IBAction)resetPressed:(id)sender{
    if (!running) {
        [stopTimer invalidate];
        stopTimer = nil;
        tableItems = [[NSMutableArray alloc] init];
        startDate = [NSDate date];
        lbl.text = @"00.00.00.0";
        running = FALSE;
    }
    else{
        [tableItems insertObject:timeString atIndex:0];
        [tableview reloadData];
    }

}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return tableItems.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    //Step 1:Check whether if we can reuse a cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

    //Step2: If there are no new cells to reuse,create a new one
    if(cell ==  nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"cell"];        
    }

    //Step 3: Set the cell text content
    cell.textLabel.text = [tableItems objectAtIndex:indexPath.row];

    //Step 4: Return the row
    return cell;

}



@end
2个回答

2
在您的updateTimer方法中,您计算了startDate和当前日期之间的时间差。您没有考虑到在秒表停止时经过的时间必须被减去。 编辑: 我建议将开始和停止之间经过的所有时间间隔相加并保存。
在您的类中添加一个NSTimeInterval timePassed属性,并按照以下方式修改代码:
- (void)viewDidLoad
{
    //...
    timePassed = 0;
}

-(IBAction)startPressed:(id)sender{
    if(!running){
        //...   
        startDate = [NSDate date];
        //...
    }else{
        NSDate *currentDate = [NSDate date];
        NSTimeInterval intervalToAdd = [currentDate timeIntervalSinceDate:startDate];
        timePassed += intervalToAdd;
    //...
}

-(void)updateTimer{
    currentDate = [NSDate date];
    NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:startDate];

    timeInterval += timePassed; // <<<<<

    NSDate *timerDate = [NSDate dateWithTimeIntervalSince1970:timeInterval];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"HH:mm:ss.S"];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]];
    timeString=[dateFormatter stringFromDate:timerDate];
    lbl.text = timeString;
}

-(IBAction)resetPressed:(id)sender{
    if (!running) {
        //...
        timePassed = 0;
        //...
    }
    //...
}

我面临的一个问题是:startDate和currentDate是字符串格式。不能直接相加。 - lakshmen
我已经编辑了我的回答,虽然我还没有尝试过,但我认为它应该可以工作 :) - Tobi
你还需要在重置方法中添加 lastStopDate = nil - Tobi
让我们在聊天中继续这个讨论:http://chat.stackoverflow.com/rooms/18913/discussion-between-lakesh-and-tobi - lakshmen
哦,等等抱歉,我看到else块中有一个打字错误,应该是lastStopDate = [NSDate date]而不是stopDate = ...你已经更正了吗? - Tobi
显示剩余2条评论

0

在我的活动项目中引用一些代码。

NSDate *timeStart = [NSDate dateWithTimeIntervalSince1970:0];
NSDate *timeEnd = [timeStart dateByAddingTimeInterval:_timePassed];

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"00:00:00"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
NSString *strDate = [dateFormatter stringFromDate:timeEnd];

_timeLabel.text = strDate;

这是我的方式。如果你想使用UILabel作为秒表, MZTimerLabel 是一个很棒的两行代码解决方案。看一下吧。
干杯。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接