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 时,转换看起来有问题(以前的视图 "jumps up" 看起来很糟糕),而在使用组转换时根本不会发生。我如何才能修复此行为并在这两个视图之间实现无滞后、跳跃等的转换?
您应该在您的视图上设置 .transition(.scale)
,而不是堆栈来显示转换
在我的应用程序中,我有这样的观点:
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 时,转换看起来有问题(以前的视图 "jumps up" 看起来很糟糕),而在使用组转换时根本不会发生。我如何才能修复此行为并在这两个视图之间实现无滞后、跳跃等的转换?
您应该在您的视图上设置 .transition(.scale)
,而不是堆栈来显示转换