简体   繁体   English

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

[英]Can I get 60fps with a 10ms drawRect?

I've asked a more general question about how CADisplayLink and drawRect behave here , but as that question doesn't have any answers yet, I thought I'd ask a more specific one: has anyone managed to get an app to run at 60fps with a drawRect method that takes ~10ms to execute? 我已经问了一个关于CADisplayLinkdrawRect如何在这里表现的更一般的问题,但由于这个问题还没有任何答案,我想我会问一个更具体的问题:有没有人设法让应用程序以60fps运行使用drawRect方法执行约10毫秒?

In my app, I configure a CADisplayLink like so: 在我的应用程序中,我像这样配置CADisplayLink

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

and the update method simply calls setNeedsDisplay on my custom UIView (as well as checking for skipped frames): 并且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];
}

This begins to skip frames as soon as drawRect takes more than 4 or 5 milliseconds. 一旦drawRect花费超过4或5毫秒,这就开始跳过帧。 For my purposes, it will need up to around 10-11ms, but I thought that given 1/60fps = 16.7ms this should be no problem. 就我的目的而言,它需要大约10-11ms,但我认为给定1 / 60fps = 16.7ms这应该没问题。

Has anyone achieved this? 有没有人实现过这个? What am I doing wrong? 我究竟做错了什么?

I just don't understand why I can't use the full 16ms between frames 我只是不明白为什么我不能在帧之间使用完整的16ms

Other stuff gets drawn when the screen refreshes besides the one view you're working on. 除了您正在处理的一个视图之外,屏幕刷新时会绘制其他内容。 (For example, the status bar clock.) Also, anything in drawRect: is not hardware accelerated. (例如,状态栏时钟。)此外, drawRect:任何内容都不是硬件加速的。 So some of that computational power is doing other stuff (background downloads, iCloud sync, etc.) 因此,一些计算能力正在做其他事情(后台下载,iCloud同步等)

You'll never reliably get 60fps using drawRect: . 使用drawRect:你永远无法可靠地获得60fps drawRect: . You can try to improve performance using caching and some of Apple's other drawing tips . 您可以尝试使用缓存和Apple的其他一些绘图技巧来提高性能。 You might also get better performance using Core Animation or SceneKit, both of which are highly optimized. 使用Core Animation或SceneKit也可以获得更好的性能,这两者都是高度优化的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM