NSTimer Lap Time iPhone

久未见 提交于 2019-12-25 05:52:09

问题


This is my code for my stopwatch. It all works except for the lap button function. How would i be able to implement a lap time where when the ib action "lap" is pressed it would store the current time in an array and list the lap times on the view?

I have already tried to create a database but this seemed far to complex for something of this nature.

#import "FirstViewController.h"

@implementation FirstViewController
@synthesize start;
@synthesize stop;
@synthesize lap;
@synthesize reset;
@synthesize lapLabel;
@synthesize stopWatchLabel;

NSDate *startDate;
NSDateFormatter *dateFormatter;
NSTimer *stopWatchTimer;
NSTimeInterval secondsAlreadyRun;


int touchCount;



-(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;

}

- (IBAction)onStartPressed:(UIButton *)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];

    touchCount +=1;
    if (touchCount == 1) 
    {
        start.hidden = YES;
        stop.hidden = NO;
        reset.hidden = YES;
        lap.hidden = NO;
        touchCount = 0;
    }

}

- (IBAction)onStopPressed:(UIButton *)sender {
    // _Increment_ secondsAlreadyRun to allow for multiple pauses and restarts
    secondsAlreadyRun += fabs([startDate timeIntervalSinceNow]);
    [stopWatchTimer invalidate];
    stopWatchTimer = nil;
    stop.hidden = YES;
    start.hidden = NO;
    reset.hidden = NO;
    lap.hidden = YES;

}

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

- (IBAction)lap:(UIButton *)sender; {
    //Lap Code will go here.

}

- (void)viewDidUnload {
    [self setStart:nil];
    [self setStop:nil];
    [self setLap:nil];
    [self setReset:nil];
    [self setLap:nil];
    [super viewDidUnload];
}
@end

回答1:


Just use a NSMutableArray stored in the class. Every time the person clicks the lap-button For instance call

NSMutableArray *lapTimes;

And in your method do:

- (IBAction)lap:(UIButton *)sender {
    double timeSinceStart = [startDate timeIntervalSinceNow];
    [lapTimes addObject:[NSNumber numberWithDouble:-timeSinceStart]];
}

timeIntervalSinceNow will return the difference in time between the NSDate object and now in seconds. If the NSDate is earlier then now (like in your case), the returned number will be negative, so you will have to invert it.



来源:https://stackoverflow.com/questions/8197430/nstimer-lap-time-iphone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!