gpt4 book ai didi

ios - 模拟器中的慢速 CADisplayLink 动画

转载 作者:行者123 更新时间:2023-11-28 15:57:09 27 4
gpt4 key购买 nike

我有两种实现相同 Sprite 动画的方法:

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

这两种方法都非常有效,并且在我的 iPhone 上看起来几乎相同(如果不完全相同的话)。但是,在模拟器中运行时,#1 似乎以设备速度进行动画处理,而 #2 似乎以明显较慢的速度进行动画处理。

当比较我的设备和模拟器的 FPS 时,设备平均约为 59.9 到 60 FPS,而模拟器显示恒定的 60 FPS;这并没有说明 #2 似乎明显变慢。

那么,为什么 #2 在 Simulator 中速度较慢?

#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
}

最佳答案

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

关于ios - 模拟器中的慢速 CADisplayLink 动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41543856/

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