NSTimer圈时间iPhone [英] NSTimer Lap Time iPhone

查看:45
本文介绍了NSTimer圈时间iPhone的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的秒表代码.除膝部按钮功能外,其他所有功能均有效.我将如何实现一个单圈时间,当按下ib动作"lap"时,它将当前时间存储在数组中并在视图上列出单圈时间?

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

推荐答案

只需使用存储在该类中的NSMutableArray.每次人单击膝上按钮时例如调用

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

NSMutableArray *lapTimes;

在您的方法中执行:

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

timeIntervalSinceNow将返回NSDate对象与现在之间的时间差(以秒为单位).如果NSDate早于现在(如您的情况),则返回的数字将为负,因此您必须将其取反.

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.

这篇关于NSTimer圈时间iPhone的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆