2010-05-17 5 views
1

나는 처음으로 iphone 앱을 완성하는 데 아주 가깝습니다. 기뻤습니다. UILabel에 현재 시간 (NSDate)을 표시하는 NSTimer를 통해 현재 시간을 사용하여 실행중인 시간 코드를 추가하려고합니다. NSDate는 시간, 분, 초, 밀리 초를 보여 주며 잘 작동합니다. 하지만 밀리 초 대신 초당 24 프레임을 표시해야합니다.NSTimer 및 NSDateFormatter를 사용하여 시간 코드 표시

문제는 초당 프레임을시, 분, 초로 100 % 동기화해야하므로 개별 타이머에 프레임을 추가 할 수 없다는 점입니다. 나는 그것을 시험해 보았지만 프레임 타이머가 날짜 타이머와 동기화되어 실행되지 않았다.

누구든지 나를 도와 줄 수 있습니까? 초당 24 프레임으로 포맷 된 날짜 타이머를 가질 수 있도록 NSDateFormatter를 사용자 정의하는 방법이 있습니까? 지금은 시간, 분, 초 및 밀리 초 형식으로 제한됩니다. 이 초, 그래서 일부는 당신에게 밀리 초를 줄 것이다 -

여기에 당신이 당신의 NSDate에서 밀리 초를 추출하는 것이

-(void)runTimer { 
// This starts the timer which fires the displayCount method every 0.01 seconds 
runTimer = [NSTimer scheduledTimerWithTimeInterval: .01 
      target: self 
      selector: @selector(displayCount) 
      userInfo: nil 
       repeats: YES]; 
} 

//This formats the timer using the current date and sets text on UILabels 
- (void)displayCount; { 

NSDateFormatter *formatter = 
[[[NSDateFormatter alloc] init] autorelease]; 
    NSDate *date = [NSDate date]; 

// This will produce a time that looks like "12:15:07:75" using 4 separate labels 
// I could also have this on just one label but for now they are separated 

// This sets the Hour Label and formats it in hours 
[formatter setDateFormat:@"HH"]; 
[timecodeHourLabel setText:[formatter stringFromDate:date]]; 

// This sets the Minute Label and formats it in minutes 
[formatter setDateFormat:@"mm"]; 
[timecodeMinuteLabel setText:[formatter stringFromDate:date]]; 

// This sets the Second Label and formats it in seconds 
[formatter setDateFormat:@"ss"]; 
[timecodeSecondLabel setText:[formatter stringFromDate:date]]; 

//This sets the Frame Label and formats it in milliseconds 
//I need this to be 24 frames per second 
[formatter setDateFormat:@"SS"]; 
[timecodeFrameLabel setText:[formatter stringFromDate:date]]; 

} 

답변

1

내가 제안 지금 사용하고 코드입니다.

그런 다음 일반 형식 문자열을 사용하여 NSString 방법 stringWithFormat :을 사용하여 값을 추가하십시오.

+0

일부 재생 후, 나는 아직도 이것을 구현하는 방법을 잘 모르겠습니다. 나는 당신이 제안한 것을 성공적으로 할 수 있었지만 실제로 초당 24 프레임의 변환을하는 수학의 전체 덩어리를 놓치고있는 것 같습니다. 초당 100 밀리 초를 표시하는 대신 매초마다 0-23의 숫자를 표시해야하며 NSTimer와 완벽하게 동기화되어 각 초가 끝나면 실제로 완료되고 다시 시작해야합니다. –

0

다음은 재 처리 용도로 사용하기에 매우 쉬운 Processing/Java 버전입니다.

String timecodeString(int fps) { 
    float ms = millis(); 
    return String.format("%02d:%02d:%02d+%02d", floor(ms/1000/60/60), // H 
               floor(ms/1000/60),  // M 
               floor(ms/1000%60),  // S 
               floor(ms/1000*fps%fps)); // F 
} 
1

NSFormatter + NSDate로 오버 헤드가 많습니다. 게다가 그것은 NSDate가 단순한 것들에 대해 "단순한"미세한 상황을 제공하지 않는 것처럼 보입니다.

- (NSString *) formatTimeStamp:(float)seconds { 
    int sec = floor(fmodf(seconds, 60.0f)); 
    return [NSString stringWithFormat:@"%02d:%02d.%02d.%03d", 
         (int)floor(seconds/60/60),   // hours 
         (int)floor(seconds/60),    // minutes 
         (int)sec,       // seconds 
         (int)floor((seconds - sec) * 1000) // milliseconds 
      ]; 
} 

// NOTE: %02d is C style formatting where: 
// % - the usual suspect 
// 02 - target length (pad single digits for padding) 
// d - the usual suspect 

이 형식에 대한 추가 정보를 원하시면 this discussion를 참조하십시오

는 Mogga 여기에 C/오브젝티브 C 변종의 멋진 포인터를 제공했다.