Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to animate images in SwiftUI, to play a frame animation

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

like image 689
arthas Avatar asked Jul 22 '19 17:07

arthas


People also ask

How do I use animation in SwiftUI?

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.

How do I animate a view in SwiftUI?

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.

How to animate a dot forever in SwiftUI?

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

How to animate the change of scale variable in SwiftUI?

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!

How do you animate cards and subviews?

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


1 Answers

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
        })
    }
}
like image 159
DreamPiggy Avatar answered Oct 15 '22 11:10

DreamPiggy