Просмотр исходного кода

Fixes loop reporting

There’s an issue in which the current loop is reported as ending when each frame in the buffer has been displayed, _not_ when each frame in the entire gif has been displayed.
Michael Liberatore 8 лет назад
Родитель
Сommit
8c3190164c
1 измененных файлов с 12 добавлено и 8 удалено
  1. 12 8
      Sources/AnimatedImageView.swift

+ 12 - 8
Sources/AnimatedImageView.swift

@@ -305,6 +305,7 @@ class Animator {
     fileprivate let maxTimeStep: TimeInterval = 1.0
     fileprivate var frameCount = 0
     fileprivate var currentFrameIndex = 0
+    fileprivate var currentFrameIndexInBuffer = 0
     fileprivate var currentPreloadIndex = 0
     fileprivate var timeSinceLastFrameChange: TimeInterval = 0.0
     fileprivate var needsPrescaling = true
@@ -315,7 +316,7 @@ class Animator {
     private var loopCount = 0
     
     var currentFrame: UIImage? {
-        return frame(at: currentFrameIndex)
+        return frame(at: currentFrameIndexInBuffer)
     }
 
     var isReachMaxRepeatCount: Bool {
@@ -426,21 +427,24 @@ class Animator {
      */
     func updateCurrentFrame(duration: CFTimeInterval) -> Bool {
         timeSinceLastFrameChange += min(maxTimeStep, duration)
-        guard let frameDuration = animatedFrames[safe: currentFrameIndex]?.duration, frameDuration <= timeSinceLastFrameChange else {
+        guard let frameDuration = animatedFrames[safe: currentFrameIndexInBuffer]?.duration, frameDuration <= timeSinceLastFrameChange else {
             return false
         }
         
         timeSinceLastFrameChange -= frameDuration
         
-        let lastFrameIndex = currentFrameIndex
-        currentFrameIndex += 1
-        currentFrameIndex = currentFrameIndex % animatedFrames.count
-
+        let lastFrameIndex = currentFrameIndexInBuffer
+        currentFrameIndexInBuffer += 1
+        currentFrameIndexInBuffer = currentFrameIndexInBuffer % animatedFrames.count
+        
         if animatedFrames.count < frameCount {
             preloadFrameAsynchronously(at: lastFrameIndex)
         }
-
-        if currentFrameIndex == 0 {
+        
+        currentFrameIndex += 1
+        
+        if currentFrameIndex == frameCount {
+            currentFrameIndex = 0
             currentRepeatCount += 1
 
             delegate?.animator(self, didPlayAnimationLoops: currentRepeatCount)