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

How to reset to frame 0 after gif is played with AnimatedGif #307

Open
morad opened this issue Mar 23, 2024 · 1 comment
Open

How to reset to frame 0 after gif is played with AnimatedGif #307

morad opened this issue Mar 23, 2024 · 1 comment

Comments

@morad
Copy link

morad commented Mar 23, 2024

Hello there,

What is the best way to reset the gif to zero frame after completing playing it? here is the code I have so far:

AnimatedImage(url: URL(string: selectedArticle.imageName), placeholderImage: .init(systemName: "Pic 7"))
.customLoopCount(1)
.runLoopMode(.common)
.transition(.fade)

@dreampiggy
Copy link
Collaborator

dreampiggy commented Mar 23, 2024

Emmm.

Can you try using the "pausable" modifier ? I think if the gif play ended with last frame is normal behavior

Actually, this internal UIKit view(SDAnimatedImageView) has API to seek to any frame index if you want.

You can grab the native UIKit view via "onViewCreate" API and call the API. For example, you can even observe the player status using KVO

AnimatedImage(url: xxx)
.onViewCreate { view in
   if let player = view.player {
       let token = self.observe(value: player, forKeyPath: \.currentFrameIndex) { changes
           let currentFrameIndex = changes[.newValue]
           if (currentFrameIndex + 1 == player.totalFrameCount) {
              // last frame
              player.stopAnimating()
           }
       }
   }
}

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

No branches or pull requests

2 participants