Skip to content

Commit

Permalink
Change to play single frame image method
Browse files Browse the repository at this point in the history
  • Loading branch information
douknow committed Aug 13, 2023
1 parent 1304d59 commit 6b989c5
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions Sources/Gifu/Classes/Animator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ public class Animator {

/// A delegate responsible for displaying the GIF frames.
private weak var delegate: GIFAnimatable!

/// Callback for when preparation is done
private var preparationBlock: (() -> Void)? = nil

/// Callback for when all the loops of the animation are done (never called for infinite loops)
private var animationBlock: (() -> Void)? = nil
Expand Down Expand Up @@ -132,7 +135,22 @@ public class Animator {
if frameStore?.isAnimatable ?? false {
displayLink.isPaused = false
} else {
delegate.animatorHasNewFrame()
let block = preparationBlock
preparationBlock = { [weak self] in
block?()

self?.showSingleFrameImage()
}

showSingleFrameImage()
}
}

// Use when source not animatable.
private func showSingleFrameImage() {
DispatchQueue.main.async { [self] in
var container = delegate as? ImageContainer
container?.image = frameStore?.currentFrameImage
}
}

Expand All @@ -151,13 +169,16 @@ public class Animator {
/// - parameter animationBlock: Callback for when all the loops of the animation are done (never called for infinite loops)
/// - parameter loopBlock: Callback for when a loop is done (at the end of each loop)
func animate(withGIFNamed imageName: String, size: CGSize, contentMode: UIView.ContentMode, loopCount: Int = 0, preparationBlock: (() -> Void)? = nil, animationBlock: (() -> Void)? = nil, loopBlock: (() -> Void)? = nil) {
self.preparationBlock = preparationBlock
self.animationBlock = animationBlock
self.loopBlock = loopBlock
prepareForAnimation(withGIFNamed: imageName,
size: size,
contentMode: contentMode,
loopCount: loopCount,
completionHandler: preparationBlock)
completionHandler: { [weak self] in
self?.preparationBlock?()
})
startAnimating()
}

Expand All @@ -171,13 +192,16 @@ public class Animator {
/// - parameter animationBlock: Callback for when all the loops of the animation are done (never called for infinite loops)
/// - parameter loopBlock: Callback for when a loop is done (at the end of each loop)
func animate(withGIFData imageData: Data, size: CGSize, contentMode: UIView.ContentMode, loopCount: Int = 0, preparationBlock: (() -> Void)? = nil, animationBlock: (() -> Void)? = nil, loopBlock: (() -> Void)? = nil) {
self.preparationBlock = preparationBlock
self.animationBlock = animationBlock
self.loopBlock = loopBlock
prepareForAnimation(withGIFData: imageData,
size: size,
contentMode: contentMode,
loopCount: loopCount,
completionHandler: preparationBlock)
completionHandler: { [weak self] in
self?.preparationBlock?()
})
startAnimating()
}

Expand Down

0 comments on commit 6b989c5

Please sign in to comment.