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