kaishin / Gifu

High-performance animated GIF support for iOS in Swift
Other
3.09k stars 279 forks source link

Add loopBlock callback for when a loop is finished #183

Closed robinst closed 1 year ago

robinst commented 2 years ago

It's called every time the last frame is shown, on every loop. This is different to animationBlock which is only called after all the loops have finished (and never for infinite loops).

Why? I have a use case where I'm playing a GIF in an infinite loop but I want to know when it has been shown at least once (first loop has been completed).

Currently, there's no good way to satisfy this use case. One way is to read loopDuration and then schedule a timer to fire when that time has elapsed, which is roughly when one loop has happened. But in addition to being inaccurate, it's very fiddly because the timer has to be paused/unpaused if the GIF is paused/unpaused.

Another way you might think would work is to use loopCount: 1 instead and then use animationBlock. Then there just start playback again for the rest of the loops. The problem with that is that there's no way to restart without preparing the frames again AFAICT.

With this new callback, it's very straightforward.

robinst commented 1 year ago

Ping

kaishin commented 1 year ago

Thank you for the PR and for your patience. While the use case might be quite niche I appreciate the effort and care put into the PR. Happy to get this merged as is.