简体   繁体   English

SwiftUI:如何根据 State Object 创建自定义 SwiftUI 转换?

[英]SwiftUI: How to create a custom SwiftUI Transition depending on a State Object?

Context : I have encountered a problem while working with SwiftUI Transitions .上下文:我在使用SwiftUI Transitions时遇到了问题。 I have a FormView containing 4 different Sub-Forms .我有一个FormView包含 4 个不同Sub-Forms When the User presses the Next-Button , the next Sub-Form View is presented with a Transition (.move -> right to left) .当用户按下Next-Button时,下一个Sub-Form View会显示一个Transition (.move -> right to left)

I also have a Back Button .我还有一个Back Button When the User presses this Button, the previous Sub-Form View is shown, currently with the same Transition (.move -> right to left) .当用户按下此按钮时,将显示先前的Sub-Form View ,当前具有相同的Transition (.move -> right to left) However, I'd like to reverse the Transition in this case to (.move -> left to right) .但是,我想在这种情况下将Transition反转为(.move -> left to right)


Code代码

enum FormStep {
    case step1, step2, step3, step4
}

struct CustomForm: View  {
    @State private var step: FormStep = .step1

    var body: some View {
        // This Button will change the step State to the previous step
        Button(action: { withAnimation { previousStep() } } { Text("Previous") }

        // This is the Right to Left Transition applied to subForm
        subForm
            .transition(.asymmetric(insertion: .move(edge: .trailing), removal: .move(edge: .leading)))

        // This Button will change the step State to the next step
        Button(action: { withAnimation { nextStep() } } { Text("Next") }
    }

    @ViewBuilder private var subForm: some View {
        switch medicationFormVM.step {
        case .step1: StepOneSubForm()
        case .step2: StepTwoSubForm()
        case .step3: StepThreeSubForm()
        case .step4: StepFourSubForm()
        }
    }
}

Question: As you can see, not matter whether you navigate back or forth, the Transition will always be right to left .问题:如您所见,无论您是向后导航还是向后导航, Transition始终是right to left How can I achieve my goal of changing the Transition depending on which Button got pressed?我怎样才能根据按下哪个Button来实现改变Transition的目标?

Here is an approach for you:这是适合您的方法:

enum FormTransition {
    case next, back
    var value: AnyTransition {
        switch self {
        case .next:
            return AnyTransition.asymmetric(insertion: .move(edge: .trailing), removal: .move(edge: .leading))
        case .back:
            return AnyTransition.asymmetric(insertion: .move(edge: .leading), removal: .move(edge: .trailing))
        }
    }
}

struct CustomForm: View  {
    
    @State private var step: FormStep = .step1
    @State private var transition: AnyTransition = FormTransition.next.value
    
    var body: some View {
        
        Button(action: { transition = FormTransition.back.value; withAnimation { previousStep() } } { Text("Previous") })
        
        subForm
            .transition(transition)
        
        Button(action: { transition = FormTransition.next.value; withAnimation { nextStep() } } { Text("Next") })
    }
    
}

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

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