Displaying timecode using NSTimer and NSDateFormatter

Posted by Chris B on Stack Overflow See other posts from Stack Overflow or by Chris B
Published on 2010-05-17T09:27:41Z Indexed on 2010/05/17 9:30 UTC
Read the original article Hit count: 352

Hi. I am very close to completing my first iphone app and it has been a joy. I am trying to add running timecode using the current time via an NSTimer displaying the current time (NSDate) on a UILabel. NSDate is working fine for me, showing hour, minute, second, milliseconds. But instead of milliseconds, I need to show 24 frames per second.

The problem is that I need the frames per second to be synced 100% with the hour, minute and second, so I can't add the frames in a separate timer. I tried that and had it working but the frame timer was not running in sync with the date timer.

Can anyone help me out with this? Is there a way to customize NSDateFormatter so that I can have a date timer formatted with 24 frames per second? Right now I'm limited to formatting just hours, minutes, seconds, and milliseconds.

Here's the code I'm using right now

-(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]];

}

© Stack Overflow or respective owner

Related posts about iphone

Related posts about xcode