简体   繁体   English

NSTimer圈时间iPhone

[英]NSTimer Lap Time iPhone

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? 我将如何实现一个单圈时间,当按下ib操作“ lap”时,它将当前时间存储在数组中并在视图上列出单圈时间?

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

Just use a NSMutableArray stored in the class. 只需使用存储在该类中的NSMutableArray。 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. timeIntervalSinceNow将返回NSDate对象与现在之间的时间差(以秒为单位)。 If the NSDate is earlier then now (like in your case), the returned number will be negative, so you will have to invert it. 如果NSDate早于现在(如您的情况),则返回的数字将为负,因此您必须将其取反。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM