【问题标题】:Slow CADisplayLink Animation In Simulator模拟器中缓慢的 CADisplayLink 动画
【发布时间】:2017-01-09 08:30:58
【问题描述】:

我有两种方法可以实现相同的精灵动画:

  1. UIImage 使用 animatedImage(with:duration:) 为包含 6 个图像的数组设置动画。
  2. UIView 及其CALayercontents 属性设置为精灵图集图像-图层的contentsRect 属性通过CADisplayLink 更新。为了确保帧速率独立,我正在累积增量时间(或displayLink.duration),直到图像发生变化。当图像发生变化时,我从累积的增量时间中减去一张图像所需的经过时间,然后循环继续。

这两种方法都很好用,在我的 iPhone 上看起来几乎相同(如果不相同)。但是,在模拟器中运行时,#1 似乎以设备速度进行动画处理,而 #2 的动画处理似乎明显变慢了。

比较我的设备和模拟器的 FPS 时,设备平均在 59.9 到 60 FPS 左右,而模拟器显示恒定的 60 FPS;这并不能说明 #2 的速度明显变慢。

那么,为什么模拟器中的 #2 会变慢?

#1 的代码

UIImage.animatedImage(with: spriteImages, duration: animationDuration)

#2 的代码

func update(_ seconds: TimeInterval) {
    accumulatedSeconds += seconds

    // `poseDuration` is `animationDuration / 6`.
    guard accumulatedSeconds >= poseDuration else { return }

    switch currentFrame {
    case 6:
        myView.layer.contentsRect = CGRect(x: 0, y: 0, width: width, height: 1)
        currentFrame = 1
    default:
        myView.layer.contentsRect = CGRect(x: width * Double(currentFrame), y: 0, width: width, height: 1)
        currentFrame += 1
    }

    accumulatedSeconds -= poseDuration
}

【问题讨论】:

    标签: ios swift animation ios-simulator cadisplaylink


    【解决方案1】:

    基本上,CADisplayLink 在模拟器中不能正常工作。仅在设备上测试。

    【讨论】:

    • 感谢您的确认,谢谢 :) 您能否详细说明为什么它不能正常工作?只是想了解我错过了什么。
    • 你没有错过任何东西。很多东西在模拟器中不起作用。这是其中之一。
    • 哈,好的。您对为什么 #1 / animatedImage(with:duration:) 没有任何可感知的渲染问题有任何想法吗?它一定也在使用某种计时器,对吧?
    • 完全不同的机制。动画通常在模拟器中运行良好。但是显示链接是基于硬件的,模拟器中没有硬件,所以必须仿真。
    猜你喜欢
    • 2012-08-10
    • 2013-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-27
    • 2012-01-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多