【问题标题】:Frames per second timecode display with NSTimer使用 NSTimer 显示每秒帧数时间码
【发布时间】:2011-06-11 23:35:27
【问题描述】:

我正在开发一个需要显示运行时间码时钟的 iPhone/iPad 应用程序。我已经让它显示正确的小时、分钟和秒,使用此代码没有问题:

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

问题是当我需要每秒显示帧数时。我知道计算1/24 * 1000 会给我一帧中有多少毫秒。我只是不知道如何使NSDateNSTimer 函数与此代码一起工作,并允许它根据运行时间码的需要尽快更新UILabel

有什么建议吗?

【问题讨论】:

    标签: objective-c ios cocoa-touch uilabel nstimer


    【解决方案1】:

    如果您的计时器以 0.01 秒的周期运行,那么它的频率是 100 帧/秒(好吧,最好说它每秒有 100 个函数调用)。但是如果你需要显示精确的时间段(因为有时定时器可能会延迟),那么你需要存储之前的调用日期,然后使用

    NSDate* new_date = [NSDate date];
    double freq = 1.0 / [new_date timeIntervalSinceDate: old_date];
    [old_date release];
    old_date = [new_date retain];
    

    【讨论】:

    • 这似乎可以很好地显示毫秒。谢谢!关于如何将其显示为每秒帧数的任何想法?我需要将数字从 0 变为 23,然后重置为零并重复。
    • for(int i=0; i
    • “for”循环可以在应用程序运行时在控制台中打印出数字 0-24,但我很难在我的 UILabel 上更新这些数字。我使用的代码是:code - (void)updateFpsDisplay { for (int i=0; icode 我也在调用一个新的计时器来触发这个方法,但它只是在更新我的 UILabel 时卡住了。
    • 我刚刚展示了这个循环,让您了解如何限制数量。
    【解决方案2】:

    这是一个相当容易重新利用的 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)%60),       // M (edit: added %60)
                                                  floor(ms/1000%60),       // S
                                                  floor(ms/1000*fps%fps)); // F
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-23
      • 2020-01-05
      • 2011-12-27
      • 1970-01-01
      • 2016-11-28
      • 1970-01-01
      • 2013-06-23
      • 2020-08-28
      相关资源
      最近更新 更多