Skip to content

Commit

Permalink
Add animation completion block (#150)
Browse files Browse the repository at this point in the history
  • Loading branch information
RichardFevrier authored and kaishin committed Nov 5, 2019
1 parent b5c0ae1 commit 7c3a9bf
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
15 changes: 11 additions & 4 deletions Source/Classes/Animator.swift
Expand Up @@ -22,6 +22,8 @@ public class Animator {

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

private var animationBlock: (() -> Void)? = nil

/// Responsible for starting and stopping the animation.
private lazy var displayLink: CADisplayLink = { [unowned self] in
Expand Down Expand Up @@ -55,6 +57,9 @@ public class Animator {
guard let store = frameStore else { return }
if store.isFinished {
stopAnimating()
if let animationBlock = animationBlock {
animationBlock()
}
return
}

Expand Down Expand Up @@ -131,12 +136,13 @@ public class Animator {
/// - parameter contentMode: The view content mode to use for the individual frames.
/// - parameter loopCount: Desired number of loops, <= 0 for infinite loop.
/// - parameter completionHandler: Completion callback function
func animate(withGIFNamed imageName: String, size: CGSize, contentMode: UIView.ContentMode, loopCount: Int = 0, completionHandler: (() -> Void)? = nil) {
func animate(withGIFNamed imageName: String, size: CGSize, contentMode: UIView.ContentMode, loopCount: Int = 0, preparationBlock: (() -> Void)? = nil, animationBlock: (() -> Void)? = nil) {
self.animationBlock = animationBlock
prepareForAnimation(withGIFNamed: imageName,
size: size,
contentMode: contentMode,
loopCount: loopCount,
completionHandler: completionHandler)
completionHandler: preparationBlock)
startAnimating()
}

Expand All @@ -147,12 +153,13 @@ public class Animator {
/// - parameter contentMode: The view content mode to use for the individual frames.
/// - parameter loopCount: Desired number of loops, <= 0 for infinite loop.
/// - parameter completionHandler: Completion callback function
func animate(withGIFData imageData: Data, size: CGSize, contentMode: UIView.ContentMode, loopCount: Int = 0, completionHandler: (() -> Void)? = nil) {
func animate(withGIFData imageData: Data, size: CGSize, contentMode: UIView.ContentMode, loopCount: Int = 0, preparationBlock: (() -> Void)? = nil, animationBlock: (() -> Void)? = nil) {
self.animationBlock = animationBlock
prepareForAnimation(withGIFData: imageData,
size: size,
contentMode: contentMode,
loopCount: loopCount,
completionHandler: completionHandler)
completionHandler: preparationBlock)
startAnimating()
}

Expand Down
14 changes: 8 additions & 6 deletions Source/Classes/GIFAnimatable.swift
Expand Up @@ -56,33 +56,35 @@ extension GIFAnimatable {
/// - parameter imageName: The file name of the GIF in the main bundle.
/// - parameter loopCount: Desired number of loops, <= 0 for infinite loop.
/// - parameter completionHandler: Completion callback function
public func animate(withGIFNamed imageName: String, loopCount: Int = 0, completionHandler: (() -> Void)? = nil) {
public func animate(withGIFNamed imageName: String, loopCount: Int = 0, preparationBlock: (() -> Void)? = nil, animationBlock: (() -> Void)? = nil) {
animator?.animate(withGIFNamed: imageName,
size: frame.size,
contentMode: contentMode,
loopCount: loopCount,
completionHandler: completionHandler)
preparationBlock: preparationBlock,
animationBlock: animationBlock)
}

/// Prepare for animation and start animating immediately.
///
/// - parameter imageData: GIF image data.
/// - parameter loopCount: Desired number of loops, <= 0 for infinite loop.
/// - parameter completionHandler: Completion callback function
public func animate(withGIFData imageData: Data, loopCount: Int = 0, completionHandler: (() -> Void)? = nil) {
public func animate(withGIFData imageData: Data, loopCount: Int = 0, preparationBlock: (() -> Void)? = nil, animationBlock: (() -> Void)? = nil) {
animator?.animate(withGIFData: imageData,
size: frame.size,
contentMode: contentMode,
loopCount: loopCount,
completionHandler: completionHandler)
preparationBlock: preparationBlock,
animationBlock: animationBlock)
}

/// Prepare for animation and start animating immediately.
///
/// - parameter imageURL: GIF image url.
/// - parameter loopCount: Desired number of loops, <= 0 for infinite loop.
/// - parameter completionHandler: Completion callback function
public func animate(withGIFURL imageURL: URL, loopCount: Int = 0, completionHandler: (() -> Void)? = nil) {
public func animate(withGIFURL imageURL: URL, loopCount: Int = 0, preparationBlock: (() -> Void)? = nil, animationBlock: (() -> Void)? = nil) {
let session = URLSession.shared

let task = session.dataTask(with: imageURL) { (data, response, error) in
Expand All @@ -91,7 +93,7 @@ extension GIFAnimatable {
print("Error downloading gif:", error.localizedDescription, "at url:", imageURL.absoluteString)
case (let data?, _, _):
DispatchQueue.main.async {
self.animate(withGIFData: data, loopCount: loopCount, completionHandler: completionHandler)
self.animate(withGIFData: data, loopCount: loopCount, preparationBlock: preparationBlock, animationBlock: animationBlock)
}
default: ()
}
Expand Down
2 changes: 1 addition & 1 deletion Supporting Files/Info.plist
Expand Up @@ -19,7 +19,7 @@
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>153</string>
<string>154</string>
<key>NSPrincipalClass</key>
<string></string>
</dict>
Expand Down

0 comments on commit 7c3a9bf

Please sign in to comment.