简体   繁体   English

SwiftUI:如何使用延迟为两个图像之间的过渡设置动画?

[英]SwiftUI: how to animate transition between two Images using a delay?

I have two images, ImageA and ImageB , and my goal is to animate a transition between them in the following manner:我有两个图像ImageAImageB ,我的目标是通过以下方式为它们之间的过渡设置动画:

  1. ImageA is loaded ImageA已加载
  2. Pause for 1 second暂停 1 秒
  3. ImageA transitions to ImageB , with a duration of 1.5 seconds ImageA转换为ImageB ,持续时间为 1.5 秒

With regular Swift, it would be using an UIView.animate closure with an animation duration and a pause.使用常规 Swift,它将使用带有动画持续时间和暂停的UIView.animate闭包。

Here is my attempt with SwiftUI, which is not showing a smooth transition, but a hard image change instead:这是我对 SwiftUI 的尝试,它没有显示平滑的过渡,而是硬图像更改:

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编辑:整个代码库可以在这里找到: TestWidget Github

You have to add withAnimation() { ... } wrapper around observed changes, which you want to be animated.您必须在观察到的变化周围添加withAnimation() { ... }包装器,您希望对其进行动画处理。

Change your code to将您的代码更改为

DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
                                withAnimation(){
                                self.shouldTransition.toggle()
                                }
                            }

You don't specify which kind of transition do you want, by default SwiftUI uses opacity...您没有指定您想要哪种过渡,默认情况下 SwiftUI 使用不透明度...

Try the following尝试以下

VStack(alignment: .leading, spacing: 0) {
    if !shouldTransition {
        Image(images.randomElement()!)
            .resizable()
            .scaledToFill()
            .onAppear {
                DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
                    self.shouldTransition.toggle()
                }
            }
            //.transition(.move(edge: .top))      // uncomment to modify transition
    }

    if shouldTransition {
        Image(images.randomElement()!)
            .resizable()
            .scaledToFill()
            //.transition(.move(edge: .top))      // uncomment to modify transition
    }
    
}.animation(.easeInOut(duration: 1.5), value: shouldTransition)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM