簡體   English   中英

如何在 SwiftUI 中為文本視圖設置動畫?

[英]How to animate a Text view in SwiftUI?

我想在基於 state 變量有條件地呈現或刪除文本視圖時為其設置動畫。

我試過了,沒有用

struct AnimatedText: View {
  @State var showingText = false
  
  var body: some View {
    VStack {

      if showingText {
        Text("Hello world")
          .animation(.easeInOut, value: showingText)
      }
      
      Button("Toggle") {
        showingText.toggle()
      }
    }
  }
}

如何為文本視圖設置動畫?

出現/消失是由容器動畫的,因此您需要將Text放入某個容器並使其具有動畫效果,例如

var body: some View {
  VStack {

    VStack {
      if showingText {
        Text("Hello world")
      }
    }.animation(.easeInOut, value: showingText)     // << here !!

    Button("Toggle") {
      showingText.toggle()
    }
  }
}

暫無
暫無

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

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