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

标签 iphone uiscrollview uikit tracking cadisplaylink

我正在尝试在常规(非 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/

相关文章:

ios - Xcode水平UIScrollView

ios - 是什么导致 UIPasteboard 中的项目被转换为 NSConcreteMutableData?

ios - UIKit Dynamics 和 UIKit Animation 有什么区别?

iphone - 苹果推送通知表现得很奇怪

iphone - Xcode for iPhone 静态库和动态库的区别

iphone - 如何在 iphone 的 ScrollView 中将中间按钮比其他按钮高 5px

objective-c - 如何在 Storyboard 中的任何 ViewController 之外设计单独的 UIView?

ios - UIAccessibilityVoiceOverStatusChanged 的​​替代品是什么

ios - glPushMatrix() 和 glPopMatrix() 抛出 GL_INVALID_OPERATION

iphone - UITextView 委托(delegate)方法未触发