SDWebImageSwiftUI icon indicating copy to clipboard operation
SDWebImageSwiftUI copied to clipboard

AnimatedImage is Animated While isAnimating Property is False

Open kevin-hv opened this issue 4 years ago • 4 comments

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)
        
    }
}

kevin-hv avatar May 26 '20 11:05 kevin-hv

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.

dreampiggy avatar May 27 '20 03:05 dreampiggy

Thank you. ☺️

kevin-hv avatar May 27 '20 05:05 kevin-hv

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 avatar Jun 01 '20 09:06 dreampiggy

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
}

dreampiggy avatar Jun 01 '20 09:06 dreampiggy