簡體   English   中英

SwiftUI:如何在條件視圖之間添加轉換?

[英]SwiftUI: How to add transitions between conditional views?

在我的應用程序中,我有這樣的看法:

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 過渡時根本不會發生。 如何修復此行為並實現這兩個視圖之間的轉換而沒有滯后、跳躍等?

您應該在視圖上設置.transition(.scale) ,而不是顯示過渡的堆棧

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM