SDWebImage / SDWebImageSwiftUI

SwiftUI Image loading and Animation framework powered by SDWebImage
https://sdwebimage.github.io/SDWebImageSwiftUI
MIT License
2.22k stars 231 forks source link

AnimatedImage is Animated While isAnimating Property is False #114

Open kevin-hv opened 4 years ago

kevin-hv commented 4 years ago

Steps to Reproduce: Create a Bool State variable, with an initial value of false Add an AnimatedImage to a View with the isAnimating property set to the State Bool which is false Build the Application

Expected Result: The animation is frozen

Actual Result: The animation is running

OS Version: 13.4.1

Device: iPhone SE (2nd generation) Simulator

Example Code:

struct ContentView: View {

    @State var isAnimating : Bool = false

    var body: some View {
        VStack{
            AnimatedImage(name: "elephant.png", isAnimating: $isAnimating)
                .scaledToFit()

            Toggle(isOn: $isAnimating){
                Text("Animating")
                .bold()
            }
        }.padding(50)

    }
}
dreampiggy commented 4 years ago

There is one logic: playWhenAppear and pauseWhenDisappear for SDAnimatedImageView

So this cause the initial value does not works for animation. I'll provide a fix.

kevin-hv commented 4 years ago

Thank you. ☺️

dreampiggy commented 4 years ago

Seems this issue caused by your SwiftUI AnimatedImage not visible...

Not so easy to fix. Because the logic is based on current animating status != isAnimating binding property. When the AnimnatedImage not visible, it actually isAnimating = false. But when visible, we can not get a immediately callback to disable that.

Maybe a temp solution, it's to turn off the autoPlayAnimatedImage property. This is new API in SDWebImage 5.8.0.

See: https://github.com/SDWebImage/SDWebImage/releases

dreampiggy commented 4 years ago

Please have a try to use the temp workaround below, I'll try to find a solid solution for this issue:

AnimatedImage(url: url)
.onViewCreate { view
    let imageView = view as! SDAnimatedImageView
    imageView. autoPlayAnimatedImage = false
}