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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

The offset animation is not functioning as expected 馃 #296

Open
rathodmayur93 opened this issue Jan 12, 2024 · 5 comments
Open

The offset animation is not functioning as expected 馃 #296

rathodmayur93 opened this issue Jan 12, 2024 · 5 comments

Comments

@rathodmayur93
Copy link

Hello,
I want to express my gratitude for developing this library; it has been working exceptionally well for my project. Lately, I've been attempting to apply an offset animation to a WebImage, but unfortunately, the image doesn't animate at all. Interestingly, when I substitute the WebImage with AsyncImage, the animation works perfectly. Does anyone have any insights into why this might be happening?

Code

struct FirstView: View {
    
    @State private var isAnimating: Bool = false
    
    var body: some View {
        ZStack {
            Color.red
            
            VStack {
                
                VStack {
                    WebImage(url: URL(string: "https://picsum.photos/200"))
                        .scaledToFit()
                        .offset(x: isAnimating ? 0 : -1000)
                        .animation(.linear(duration: 2.0), value: isAnimating)
                    
                    AsyncImage(url: URL(string: "https://picsum.photos/200"))
                        .scaledToFit()
                        .offset(x: isAnimating ? 0 : -1000)
                        .animation(.linear(duration: 2.0), value: isAnimating)
                    
                }
                .opacity(isAnimating ? 1 : 0)
                .animation(.linear(duration: 2.0), value: isAnimating)
            }
            .ignoresSafeArea()
            .onAppear {
                isAnimating = true
            }
        }
    }
}

#Preview {
    FirstView()
}

Output

Simulator.Screen.Recording.-.iPhone.15.Pro.-.2024-01-12.at.15.00.20.mp4
@dreampiggy
Copy link
Collaborator

Try again with 3.0.0-beta2 ?

@foxmayurrathod
Copy link

Hi @dreampiggy
Thank you for getting back to me. Even after upgrading to 3.0.0 beta 3, I'm still encountering the same problem. Would you please confirm if it's working properly on your side?

@dreampiggy
Copy link
Collaborator

dreampiggy commented Jan 16, 2024

A workaround is to use the explicit animation in SwiftUI.

The WebImage itself contains complicated structure because of animated GIF support (which means, it replace itself with new body when timer of next frame triggered). It's not a simple shape view.

Can you have a try with withAnimation closure ? https://developer.apple.com/tutorials/swiftui/animating-views-and-transitions

WebImage(url: URL(string: "https://picsum.photos/200"))
    .scaledToFit()
    .offset(x: isAnimating ? 0 : -1000)
    .animation(.linear(duration: 2.0), value: isAnimating)
.onAppear {
 withAnimation {
    isAnimating = true
  }
}

For AnimatedImage, it use UIViewRepresentable, it's magic handled by SwiftUI X UIKit support and use another render technique, so it's work without additional help.

@foxmayurrathod
Copy link

@dreampiggy, I attempted the solution you suggested, but unfortunately, it is still not working for me

@fuziki
Copy link

fuziki commented Feb 24, 2024

Adding the geometryGroup modifier to the WebImage could help resolve the issue. This modifier is effective in addressing conflicts within the animation hierarchy.

If you're using iOS 17 or later, you can try the following:

WebImage(url: url)
    .scaledToFit()
    .geometryGroup()

For versions prior to iOS 17, consider trying transformEffect(.identity):

WebImage(url: URL(string: "https://picsum.photos/200"))
    .scaledToFit()
    .transformEffect(.identity)

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

4 participants