【问题标题】:SwiftUI: How to add transitions between conditional views?SwiftUI:如何在条件视图之间添加转换?
【发布时间】:2020-05-25 11:10:20
【问题描述】:

在我的应用中,我有这样的看法:

import SwiftUI

let defaults = UserDefaults.standard

struct ContentView: View {
    @State var haveSeenIntroduction: Bool = defaults.bool(forKey: "haveSeenIntroduction")

    var body: some View {
        ZStack {
            if self.haveSeenIntroduction {
                DefaultView()
            } else {
                IntroductionView(haveSeenIntroduction: $haveSeenIntroduction)
            }
        }.transition(.scale)
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

在“IntroductionView”中我有这样的按钮:

Button(action: {
    let generator = UINotificationFeedbackGenerator()
    generator.notificationOccurred(.success)

    let defaults = UserDefaults.standard

    defaults.set(true, forKey: "haveSeenIntroduction")

    withAnimation {
        self.haveSeenIntroduction = true
    }
})

当我按下按钮时,在父视图中使用 ZStack 时,过渡看起来有问题(前一个视图“跳起来”并且看起来很糟糕),而使用 Group 过渡时根本不会发生。如何解决此问题并实现这两个视图之间的过渡而不会出现滞后、跳跃等问题?

【问题讨论】:

    标签: ios swift swiftui transition


    【解决方案1】:

    您应该在视图上设置.transition(.scale),而不是在堆栈中显示转换

    【讨论】:

    • 我之前尝试过,但是用 Group 代替,看起来正确的解决方案是使用 ZStack 和视图本身的转换,谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-06
    • 1970-01-01
    • 1970-01-01
    • 2020-06-07
    • 2020-10-29
    相关资源
    最近更新 更多