Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix can't play single frame gif #192

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 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 @@ -131,6 +134,23 @@ public class Animator {
func startAnimating() {
if frameStore?.isAnimatable ?? false {
displayLink.isPaused = false
} else {
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 @@ -149,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 @@ -169,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