【问题标题】:Iphone stopwatch - start, stop and start but doesn't start off with where it left offIphone秒表 - 开始,停止和开始,但不会从停止的地方开始
【发布时间】:2012-11-01 10:15:12
【问题描述】:

我正在尝试制作秒表应用程序,但在使其正常工作时遇到了问题。当我启动秒表并停止并再次启动时,它不会从停止的地方继续。它继续运行,就好像它没有停止一样。需要一些指导以使其正常工作。我从早上开始就一直在尝试,我所做的其他功能类似于苹果的秒表,只是这让我很烦。感谢任何帮助...

代码如下所示:

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

【问题讨论】:

    标签: ios xcode ios6 nstimer stopwatch


    【解决方案1】:

    在您的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 是字符串格式。不能只添加它们..
    • 我的想法是将当前值作为差异存储在标签中(首先初始化为零),当您再次按下开始时,它将与当前值相加。但你必须添加这一行: startDate = currentDate;到 startPressed 函数...
    • 我已经编辑了我的答案,我还没有尝试过,但我认为它应该可以工作:)
    • 您还必须将lastStopDate = nil 添加到您的重置方法中
    【解决方案2】:

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

    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 是一个很棒的两行解决方案。看看吧。

    干杯。

    【讨论】:

      猜你喜欢
      • 2020-12-09
      • 1970-01-01
      • 1970-01-01
      • 2012-01-01
      • 1970-01-01
      • 2010-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多