3

I would like to animate a Text view whenever it is conditionally presented or removed based upon a state variable.

I've tried, to no avail

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()
      }
    }
  }
}

How can I animate the Text view?

1 Answer 1

2

Appear/disappear is animated by container, so you need to place Text into some container and make it animatable, like

var body: some View {
  VStack {

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

    Button("Toggle") {
      showingText.toggle()
    }
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.