簡體   English   中英

在模擬器中慢速CADisplayLink動畫

[英]Slow CADisplayLink Animation In Simulator

我有兩種方法可以實現相同的精靈動畫:

  1. 一個UIImage ,它使用animatedImage(with:duration:)為6個圖像的數組設置animatedImage(with:duration:)
  2. UIView以其CALayercontents屬性設置為一個子畫面圖譜圖像圖層的contentsRect屬性經由更新CADisplayLink 為了確保幀速率獨立性,我累積了增量時間(或displayLink.duration ),直到應更改圖像為止。 當圖像改變時,我從累積的增量時間中減去一幅圖像所需的經過時間,然后循環繼續進行。

兩種方法都很好用,並且在我的iPhone上看起來幾乎相同(如果不相同)。 但是,在Simulator中運行時,#1似乎以設備速度進行動畫處理,而#2似乎以較慢的速度進行動畫處理。

比較我的設備和模擬器的FPS時,設備平均約為59.9到60 FPS,而Simulator則顯示恆定的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在模擬器中不能很好地工作。 僅在設備上測試。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM