在下面的示例中,文本及其ID在按下按钮时会更改文本视图。因此,由于更新的ID,Swiftui应该将文本视图视为新视图,应该吗?那为什么...

问题描述 投票:0回答:1
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 += "!" } } } } }
ios swift xcode swiftui
1个回答
0
投票

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.