I have two images, ImageA and ImageB, and my goal is to animate a transition between them in the following manner:
ImageAis loaded- Pause for 1 second
ImageAtransitions toImageB, with a duration of 1.5 seconds
With regular Swift, it would be using an UIView.animate closure with an animation duration and a pause.
Here is my attempt with SwiftUI, which is not showing a smooth transition, but a hard image change instead:
VStack(alignment: .leading, spacing: 0) {
Image(images.randomElement()!)
.resizable()
.scaledToFill()
.onAppear {
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
withAnimation() { //added
self.shouldTransition.toggle()
}
}
if shouldTransition {
Image(images.randomElement()!)
.resizable()
.animation(.easeInOut(duration: 1.5))
.scaledToFill()
}
}
What is missing in my implementation to animate the image transition?
Edit: the entire codebase can be found here: TestWidget Github