2011-11-19 69 views
0

這是我的秒錶。除了單圈按鈕功能以外,它都可以工作。我怎麼能夠實現一圈時間,當按下ib操作「lap」時,它會將當前時間存儲在一個數組中,並在視圖中列出單圈時間?NSTimer Lap Time iPhone

我已經嘗試創建一個數據庫,但對於這種性質的事情來說這似乎很複雜。

#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 
+0

哦,很好,再次感謝你繼續提供幫助。我仍然在高中,並沒有採取任何類,或有很多客觀代碼的經驗,所以感謝與我同在。我正在考慮採取秒已經運行,並將其轉換爲一個字符串,我可以顯示它在一個數組中,然後做一些數學,在我點擊圈按鈕後,它會顯示它,然後重新啓動或減去下一圈時間的時間。我可能不得不創建另一個NSTimeInterval,它使用secondsAlready運行,然後在我點擊每一圈按鈕後重置計數。你有沒有想要做什麼即時通訊? –

+0

幾乎就像iOS自帶的秒錶應用程序一樣,但當我有類似的東西時,我會在它之上構建它。謝謝! –

+0

你的問題很不清楚,你在尋求什麼幫助;我不知道是不是要在'lap'中給一個字符串賦值(這似乎是基於代碼註釋)。請修改您的帖子,以添加更多關於您想要解決的問題的詳細信息。謝謝。 :) –

回答

0

只需使用存儲在類中的NSMutableArray即可。每次人點擊了一圈按鈕 例如呼叫

NSMutableArray *lapTimes; 

而在你的方法去做:

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

timeIntervalSinceNow會及時返回的NSDate對象之間,現在在幾秒鐘的差異。如果NSDate現在是早些時候(就像你的情況),返回的數字將是負數,所以你將不得不倒置它。