gpt4 book ai didi

iphone - 跟踪 UIScrollView 时,带有 NSRunLoopCommonModes 的 CADisplayLink 不会在每一帧执行?

转载 作者:行者123 更新时间:2023-12-03 21:17:46 26 4
gpt4 key购买 nike

我正在尝试在常规(非 OpenGL)iPhone 应用程序中的每个帧上执行一小段代码。目标是拥有一个帧计数器,以便我可以测量(而不是猜测)应用程序中的慢点并修复它们以创建更好的用户体验。

到目前为止,我所做的是为所有模式(包括跟踪)注册 CADisplayLink:

CADisplayLink *displayLink = [[CADisplayLink displayLinkWithTarget:self selector:@selector(frame)] retain];
[displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];

执行的代码如下所示(lastDraw 是一个 NSDate* 并计数一个 int):

- (void) frame {
NSDate *newDate = [NSDate date];

if([newDate timeIntervalSinceDate:lastDraw] >= 1) {
label.text = [NSString stringWithFormat:@"%d FPS", count];

count = 0;
[lastDraw release];
lastDraw = [newDate retain];
}

count++;
}

这里的想法是每秒更新一次有关过去一秒绘制了多少帧的信息。

这似乎工作正常,但在 UIScrollView 中滚动时,数字肯定会关闭:如果我像普通人一样滚动,那么帧速率似乎是有意义的。然而,如果我剧烈地上下滚动,那么显示的速率会下降到 1 或 2 fps,但显然每秒绘制的帧数不止一帧。

当我像这样更改框架方法时:

- (void) frame {
label.text = [NSString stringWithFormat:@"%f", displayLink.timestamp];
}

很明显,当我滚动时,很少有事件被触发。

我在这里和网上阅读了很多有关 UIScrollViews 和 NSRunLoopCommonModes 与 NSDefaultRunLoopMode 的文章,但没有找到有人描述我的问题。我发现的最接近的是这个 question

Remember that if a CADisplayLink is unable to fire for a screen refresh (due to the main thread being busy doing things like drawing the previous frame) then it may skip it, as it won't have enough time to complete.

我似乎在官方 documentation 中找不到任何证据。 ,不过,希望我做错了什么......

如果有任何区别,则相关设备运行的是 iOS 4.3.3。

最佳答案

我在票证 #10848893 http://bugreport.apple.com/ 中向 Apple 验证了这一点看来这确实是 iOS 5.1 种子 3 中修复的一个错误,我无法重现该问题。

关于iphone - 跟踪 UIScrollView 时,带有 NSRunLoopCommonModes 的 CADisplayLink 不会在每一帧执行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9151744/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com