views:

454

answers:

1

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

}
+1  A: 

I would suggest that you extract the milliseconds from your NSDate - this is in seconds, so the fraction will give you milliseconds.

Then just use a plain format string to append the value using NSString method stringWithFormat:.

Paul Lynch
After some playing around, I'm still not sure how to implement this. I have been able to successfully do what you suggested, but it seems I'm missing a whole chunk of math that actually does the 24 frames per second conversion. Instead of showing 100 milliseconds per second, I need to show the numbers run through 0-23 every second and I need it to be in perfect sync with the NSTimer so that it actually completes and restarts when each second is over.
Chris B