Gifu
Gifu copied to clipboard
Notification about animation end
Hey, thanks for such a cool lib! As I can see, now I am not able to get notified when animation ended, right? My goal is to start second animation right after first was played.
Best regards, Alex
@alexth Currently the animation loops automatically. I am not sure I understand how is that not what you are after.
A user of your lib may want to use a gif as a part of a more complex flow, knowing when the animation has ended is a really cool and powerful feature.
@halonsoluis Fair enough. I do not have time to work on features that I don't need but PRs would be very welcome.
I did something like this:
class CustomGIFImageView : UIImageView, GIFAnimatable {
private let completionQueue = DispatchQueue(label: "com.myapp.gif-queue")
private var completionBlock = {}
func startAnimatingGIF(withCompletionHandler callback: @escaping () -> ()) {
startAnimatingGIF()
completionBlock = callback
completionQueue.async {
while(self.isAnimatingGIF) {}
DispatchQueue.main.async {
self.completionBlock()
}
}
}
/// A lazy animator.
public lazy var animator: Animator? = {
return Animator(withDelegate: self)
}()
/// Layer delegate method called periodically by the layer. **Should not** be called manually.
///
/// - parameter layer: The delegated layer.
override public func display(_ layer: CALayer) {
updateImageIfNeeded()
}
}
It works fine in my app
Worst solution @andreabar, while(self.isAnimatingGIF) {}
this line of code will use CPU at 100%.
I'm not using this library anymore and I know that running a loop (even if it has an empty body) every tick it's very CPU consuming. One solution could be put the thread to sleep for some milliseconds (since the code is running on a queue that is not the main thread that should not be an issue). It would reduce the CPU usage. However this could led to some delay after the moment when the animation ends and the completionBlock call.
I didn't find any better solution other than that. Oh well, actually I did found a much better solution: Do not use GIF at all. There are some other very elegant ways to put something animated in a iOS app.