ios - 我可以使用 10ms drawRect 获得 60fps 吗?

标签 ios uiview cadisplaylink

我问了一个更一般的问题,关于 CADisplayLinkdrawRect 的行为 here ,但由于这个问题还没有任何答案,我想我会问一个更具体的问题:有没有人设法让一个应用程序以 60fps 的速度运行,使用 drawRect 方法需要大约 10 毫秒执行?

在我的应用中,我配置了一个 CADisplayLink,如下所示:

displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(update)];
[displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];

update 方法只是在我的自定义 UIView 上调用 setNeedsDisplay(以及检查跳过的帧):

- (void)update
{
    static CFTimeInterval timestamp = 0;

    // check for skipped frame
    CFTimeInterval newTimestamp = displayLink.timestamp;
    if ((newTimestamp - timestamp) > (displayLink.duration * (displayLink.frameInterval + 0.5)))
        NSLog(@"Frame skipped!");
    timestamp = newTimestamp;

    [myView setNeedsDisplay];
}

一旦 drawRect 花费超过 4 或 5 毫秒,它就会开始跳帧。就我的目的而言,它最多需要 10-11 毫秒左右,但我认为给定 1/60fps = 16.7 毫秒这应该没问题。

有人做到了吗?我做错了什么?

最佳答案

I just don't understand why I can't use the full 16ms between frames

除了您正在处理的一个 View 之外,屏幕刷新时还会绘制其他内容。 (例如,状态栏时钟。)此外,drawRect: 中的任何内容都不是硬件加速的。所以一些计算能力正在做其他事情(后台下载、iCloud 同步等)

使用 drawRect: 永远无法可靠地获得 60fps。您可以尝试使用缓存和 Apple 的其他一些方法来提高性能 drawing tips .您还可以使用 Core Animation 或 SceneKit 获得更好的性能,它们都经过高度优化。

关于ios - 我可以使用 10ms drawRect 获得 60fps 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27494390/

相关文章:

ios - OpenGL 渲染在 App Backgrounded 后不恢复

ios - 如何使用 for-in-loop 在 CADisplayLink 中创建多个对象

ios - 检查 NSArray 是否包含具有特定属性的对象

ios - cocos2d音频文件问题

ios - 控件变得模糊并隐藏在 xcode 的 Storyboard 中

ios - 如何以编程方式更改 UISearchBar 或 UITableViewHeader 的 UIFont?

ios - 在 UIView 上绘制一条 5 像素的线

ios - 从运行循环中删除 CADisplayLink 时,游戏有时会崩溃

ios - 字符串到 NSDate Swift2

swift - 获取 UIView/UIImageView 的特定角坐标,而不管其旋转