Gifu icon indicating copy to clipboard operation
Gifu copied to clipboard

Notification about animation end

Open olexth opened this issue 8 years ago • 6 comments

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

olexth avatar May 04 '16 10:05 olexth

@alexth Currently the animation loops automatically. I am not sure I understand how is that not what you are after.

kaishin avatar May 28 '16 17:05 kaishin

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 avatar Jul 29 '16 20:07 halonsoluis

@halonsoluis Fair enough. I do not have time to work on features that I don't need but PRs would be very welcome.

kaishin avatar Sep 30 '16 19:09 kaishin

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

andreabar avatar Apr 03 '17 15:04 andreabar

Worst solution @andreabar, while(self.isAnimatingGIF) {} this line of code will use CPU at 100%.

sharad-paghadal avatar Jul 31 '18 11:07 sharad-paghadal

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.

andreabar avatar Jul 31 '18 12:07 andreabar