2011-11-19 2 views
0

이것은 내 스톱워치 코드입니다. 무릎 버튼 기능을 제외한 모든 기능이 작동합니다. 어떻게하면 무릎 시간을 구현할 수 있을까요? ib 액션 "랩"을 누르면 현재 시간이 배열에 저장되고 랩 타임이 뷰에 나열됩니다.NSTimer 랩타임 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을 만들어야 할 수도 있습니다. 이미 실행 한 다음 모든 랩 버튼을 클릭 한 후 카운트를 재설정합니다. 내가 무엇을하려고하는지 알지? –

+0

iOS와 함께 제공되는 스톱워치 앱과 거의 비슷하지만 유사한 기능을 갖춘 후에는 그 위에 빌드하려고했습니다. 감사! –

+0

귀하가 궁금한 점은 도움이 필요한 내용과 관련하여 매우 명확하지 않습니다. 'lap'의 문자열에 무언가를 할당하는 것이지 모르겠다. (코드 주석을 기반으로 한 것 같다.) 문제를 해결하려는 정확한 정보를 추가하려면 게시물을 수정하십시오. 감사. :) –

답변

0

클래스에 저장된 NSMutableArray 만 사용하면됩니다. 때마다 사람이 예를 호출

NSMutableArray *lapTimes; 

을 위해 그리고 당신의 방법에 랩 버튼 를 클릭은 수행

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

timeIntervalSinceNow이있는 NSDate 객체 사이에 현재 시간 (초)의 차이를 반환합니다. NSDate가 이전의 경우 (현재의 경우와 마찬가지로) 반환 된 숫자는 음수가되므로 반전해야합니다.