Text
视图视为新视图,应该吗?那么为什么过渡不触发呢?
结构TextTransitionView:查看{ @State Private var text:string =“你好,世界!”
private var textTransition: AnyTransition {
.asymmetric(
insertion: .move(edge: .bottom).combined(with: .opacity),
removal: .move(edge: .top).combined(with: .opacity)
)
}
var body: some View {
Text(text)
.transition(textTransition)
.id(text)
Button("Change Text") {
self.text += "!"
}
}
}为每个周期都有新的景观:
struct TextTransitionView: View {
@State private var text: String = "Hello, World!"
private var textTransition: AnyTransition {
.asymmetric(
insertion: .move(edge: .bottom).combined(with: .opacity),
removal: .move(edge: .top).combined(with: .opacity)
)
}
var body: some View {
VStack {
Text(text)
.transition(textTransition)
.id(text) // Ensures the Text view has a new identity
Button("Change Text") {
withAnimation {
self.text += "!"
}
}
}
}
}