Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Frames per second timecode display with NSTimer

I am working on an iPhone/iPad app that needs to display a running timecode clock. I have gotten it to display the correct hours, minutes, and seconds with no problem using this code:

    - (void) viewDidLoad {
        // Start the Timer method for here to start it when the view loads.
            runTimer = [NSTimer scheduledTimerWithTimeInterval: .01 target: self selector: @selector(updateDisplay) userInfo: nil repeats: YES];
    }

- (void)updateDisplay {
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        NSDate *date = [NSDate date];

        // Display each hour, minute, second and frame.
        [formatter setDateFormat:@"hh"];
        [timecodeHourLabel setText:[formatter stringFromDate:date]];

        [formatter setDateFormat:@"mm"];
        [timecodeMinuteLabel setText:[formatter stringFromDate:date]];

        [formatter setDateFormat:@"ss"];
        [timecodeSecondLabel setText:[formatter stringFromDate:date]];
}

The issue is when I need to display frames per second. I know that calculating 1/24 * 1000 gives me how many milliseconds are in one frame. I just don't know how to make the NSDate and NSTimer functions work with this code and allow it to update a UILabel as quickly as needed for running timecode.

Any suggestions?

like image 467
Brian Avatar asked Jan 24 '26 18:01

Brian


1 Answers

If your timer is running with the period of 0.01 sec, then it's frequency is 100 frames/sec (well, it's better to say it has 100 function calls per second). But if you need to display the precise time period (cause sometimes the timer may be delayed), then you need to store the previous call date and then use

NSDate* new_date = [NSDate date];
double freq = 1.0 / [new_date timeIntervalSinceDate: old_date];
[old_date release];
old_date = [new_date retain];
like image 108
Max Avatar answered Jan 27 '26 06:01

Max



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!