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

Add loopBlock callback for when a loop is finished #183

Merged
merged 1 commit into from
Feb 3, 2023

Conversation

robinst
Copy link
Contributor

@robinst robinst commented Oct 7, 2022

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.

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.
@@ -136,9 +145,12 @@ public class Animator {
/// - parameter size: The target size of the individual frames.
/// - 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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that the parameter name here was outdated, I changed it along with a more descriptive ... description :)

@robinst
Copy link
Contributor Author

robinst commented Dec 29, 2022

Ping

@kaishin
Copy link
Owner

kaishin commented Feb 3, 2023

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.

@kaishin kaishin merged commit c8c8cd8 into kaishin:master Feb 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants