2011-11-27 1 views
0

사용자를 위해 카운트 할 타이머를 만드는데 관심이 있습니다.시간, 분 초, 초를 지원하는 카운트 업 타이머를 만듭니다. iPhone sdk

모든 정수 변수를 별도로 추적해야하는지, 아니면 날짜 포맷터를 사용할 수 있는지 궁금합니다.

나는 현재 초과 -updateLabel 메소드를 호출하는 -scheduledTimerfoo를 사용하고 있지만, 그것은 100 초 후에 무서운 조금 보인다. 나는 일종의 "시간 : 분 : 초 : 초"와 같이 표시 할 것입니다.

건배

답변

4

NSDateFormatter은 날짜, 시간의 간격을 포맷입니다. 이를 수행하는 더 좋은 방법은 타이머를 시작할 때 기록하는 것입니다. 매초마다 타이머를 시작한 이후 경과 한 시간을 레이블로 업데이트하십시오.

- (void)startTimer { 
    // Initialize timer, with the start date as the userInfo 
    repeatTimer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(updateLabel) userInfo:[NSDate date] repeats:YES]; 
} 

- (void)updateLabel:(NSTimer *)timer { 
    // Get the start date, and the time that has passed since 
    NSDate *startDate = (NSDate *)[timer userInfo]; 
    NSTimeInterval timePassed = [[NSDate date] timeIntervalSinceDate:startDate]; 

    // Convert interval in seconds to hours, minutes and seconds 
    int hours = timePassed/(60 * 60); 
    int minutes = (timePassed % (60 * 60))/60; 
    int seconds = ((timePassed % (60 * 60)) % 60); 
    NSString *time = [NSString stringWithFormat:@"%i:%i:%i", hours, minutes, seconds]; 

    // Update the label with time string 
} 
관련 문제