ios - iOS UI 以什么帧速率运行动画?

标签 ios

我一直在努力解决这个问题,人们说是 60fps,但据我所知,没有确凿的证据证明这一点?

最佳答案

您可以创建一个显示链接 ( CADisplayLink ),该链接将在屏幕更新时调用。

创建一些属性来跟踪显示链接和变量以计算帧速率:

@property (nonatomic, strong) CADisplayLink *displayLink;
@property (nonatomic) CFTimeInterval startTime;
@property (nonatomic) NSInteger frameCount;

然后您可以使用方法来启动、停止和处理显示链接:

- (void)startDisplayLink
{
    self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(handleDisplayLink:)];
    self.startTime = CACurrentMediaTime();
    [self.displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
}

- (void)stopDisplayLink
{
    [self.displayLink invalidate];
    self.displayLink = nil;
}

// This handler will update a label on the screen with the frame rate once per second

- (void)handleDisplayLink:(CADisplayLink *)displayLink
{
    self.frameCount++;

    CFTimeInterval now = CACurrentMediaTime();
    CFTimeInterval elapsed = now - self.startTime;

    if (elapsed >= 1.0) {
        CGFloat frameRate = self.frameCount / elapsed;

        // either, like below, update a label that you've added to the view or just log the rate to the console

        self.frameRateLabel.text = [NSString stringWithFormat:@"%.1f", frameRate];

        self.frameCount = 0;
        self.startTime = now;
    }
}

然后只需启动显示链接(调用 startDisplayLink),上面的处理程序就会报告计算出的速率。

与 Instruments 相比,我个人更喜欢这个,因为我发现 Instruments 本身对性能的影响比上面的代码更大(使用 Instruments,您的帧速率可能看起来比实际情况要低)。显然,上述代码也会影响性能,但影响不大。此外,上面的内容让您可以在不受计算机限制的设备上测量性能:您始终应该在物理设备而不是模拟器上测量性能。

在回答关于达到的帧率的问题时,上面将说明在简单的动画中,一般可以达到60 fps的帧率,但是动画越复杂/数量越多,达到的帧率就会越低.

关于ios - iOS UI 以什么帧速率运行动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25118875/

相关文章:

ios - 关闭应用程序后禁用 UILocalNotifications

ios - Swift - 对于 iOS 7/iOS 8 及更高版本同时使用 UIAlertController 和 UIAlertView

ios - 将数据从一页获取到另一页

ios - UIAlertView 转换不适用于 iOS7

ios - 当应用程序的状态处于非事件状态时是否可以获得本地通知

ios - 按钮中未捕获的异常

ios - 是否可以在不委托(delegate)的情况下使用类似于 webViewFinishLoad 的功能?

ios - UIScrollView,无需一直向下滚动即可检测可滚动区域高度

ios - NSURLConnection 无响应

objective-c - 有大量资源的应用程序加载缓慢