简体   繁体   English

SwiftUI中两个嵌入视图之间的过渡效果

[英]Transition Effect Between Two Embedded Views in SwiftUI

If I have a structure like the following:如果我有如下结构:

@State var currentView = "first"

VStack {
    if (state == "first") {
        View1()
    } else if (state == "second") {
        View2()
    } else {
        View3()
    }
}

And a View1:还有一个 View1:

@Published var currentView: String

Button("to second") {
    currentView = "second"
}

How can I make sure that View2 slides from the left while replacing View1 ?如何确保View2在替换View1时从左侧滑动? Can I dynamically define which side the next view slides in from?我可以动态定义下一个视图从哪一侧滑入吗?

  1. You need to attach an animation to the binding that you pass to your views您需要将 animation 附加到您传递给视图的绑定
  2. Your views will need to have an @Binding property that they will mutate when the button is tapped您的视图需要有一个@Binding属性,当点击按钮时它们会发生变化
  3. Then you can attach a transition to your views, you can control the edge of the transition if you use .move (define which side the view slides in from)然后您可以将过渡附加到您的视图,如果您使用.move可以控制过渡的边缘(定义视图从哪一侧滑入)

I made your views take the whole screen and have a color, so that the transition is more obvious.我让你的视图占据了整个屏幕并有颜色,这样过渡更明显。 I also added a duration to each transition, you can modify it or remove it.我还为每个过渡添加了持续时间,您可以修改或删除它。

import SwiftUI

struct ContentView: View {
    @State var currentView = "first"

    var body: some View {
        VStack {
            if (currentView == "first") {
                View1(currentView: $currentView.animation())
            } else if (currentView == "second") {
                View2(currentView: $currentView.animation())
            } else {
                View3(currentView: $currentView.animation())
            }
        }
    }

}

struct View1: View {

    @Binding var currentView: String

    var body: some View {
        ZStack {
            Rectangle().fill(Color.red)
            VStack {
                Text("View1")
                Button("to second") {
                    self.currentView = "second"
                }
            }

        }
        .transition(.asymmetric(insertion: .move(edge: .leading), removal: .move(edge: .trailing)))
        .animation(.linear(duration: 2))

    }
}

struct View2: View {
    @Binding var currentView: String

    var body: some View {
        ZStack {
            Rectangle().fill(Color.yellow)
            VStack {
                Text("View2")
                Button("to third") {
                    self.currentView = "third"
                }
            }

        }
        .transition(.asymmetric(insertion: .move(edge: .trailing), removal: .move(edge: .leading)))
        .animation(.linear(duration: 2))

    }
}

struct View3: View {

    @Binding var currentView: String

    var body: some View {
        ZStack {
            Rectangle().fill(Color.pink)
            VStack {
                Text("View3")
                Button("to first") {
                    self.currentView = "first"
                }
            }

        }
        .transition(.asymmetric(insertion: .move(edge: .bottom), removal: .move(edge: .top)))
        .animation(.linear(duration: 2))
    }
}

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

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