I want to animate images in SwiftUI's Image view
First, I tried creating some variables and a function to toggle the Image("imageVariable"). It changes but there is no animation even tried the withAnimation { }
method
Secondly, I tried to use a UIKit view. Here, the animation works but I can't apply the resizable()
modifier or a set a fixed frame
var images: [UIImage]! = [UIImage(named: "pushup001")!, UIImage(named: "pushup002")!]
let animatedImage = UIImage.animatedImage(with: images, duration: 0.5)
struct workoutAnimation: UIViewRepresentable {
func makeUIView(context: Self.Context) -> UIImageView {
return UIImageView(image: animatedImage)
}
func updateUIView(_ uiView: UIImageView, context: UIViewRepresentableContext<workoutAnimation>) {
}
}
struct WorkoutView: View {
var body: some View {
VStack {
workoutAnimation().aspectRatio(contentMode: .fit)
}
}
}
In method 1 I can change the image but not animate, while, in method 2 I can animate but not control it's size
Change the animation type from easeInOut to spring() . SwiftUI includes basic animations with predefined or custom easing, as well as spring and fluid animations. You can adjust an animation's speed, set a delay before an animation starts, or specify that an animation repeats.
There are two mechanisms of animating views in SwiftUI - implicit and explicit. Implicit animations are specified by attaching the Animation modifier to the view to be animated. Explicit animations are specified using withAnimation block and specifying the animations inside the closure. Implicit animations are used in this article.
In SwiftUI, whenever you want to animate anything just put it inside withAnimation block and see the magic! This still animates the dot for only one time. We want this to run forever! Just tell the animator to repeat the animation forever and it’s as simple as that! Super easy! Wait what? This does not look good. Let’s add delay
To animate the change of scale variable, we used withAnimation block. Changed the value of scale from 0.5 to 1. In SwiftUI, whenever you want to animate anything just put it inside withAnimation block and see the magic! This still animates the dot for only one time. We want this to run forever!
Use animation to implicitly say how each card and its subviews should animate. Another way of doing this would be by using explicit animations: But for the sake of simplicity, we’ll always use the default animation, although with a different delay (you’ll see later).
If you want a robust and cross-platform SwiftUI implementation for animated images, like GIF/APNG/WebP, I recommend using SDWebImageSwiftUI. This framework is based on exist success image loading framework SDWebImage and provides a SwiftUI binding.
To play the animation, use AnimatedImage
view.
var body: some View {
Group {
// Network
AnimatedImage(url: URL(string: "https://raw.githubusercontent.com/liyong03/YLGIFImage/master/YLGIFImageDemo/YLGIFImageDemo/joy.gif"))
.onFailure(perform: { (error) in
// Error
})
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With