16

I‘m currently playing around with SwiftUI. In SwiftUI it‘s possible, to animate a State change for example like so:

struct Foo: View {
    @State private var show = false

    var body: some View {
        VStack {
            if show {
                Text("Foo")
            }
            Button(action: {
                withAnimation {
                    self.show.toggle()
                }
            }) {
                Text(show ? "Hide" : "Show")
            }
        }
    }
}

But if I have for example a TextField:

struct Foo: View {
    @State private var text = ""

    var body: some View {
        VStack {
            TextField($text, placeholder: Text("Foo")) {
                print("editing ended")
            }
            if !text.isEmpty {
                Button(action: {}) {
                    Text("Done")
                }
            }
        }
    }
}

I‘m not able to find a way to animate this change, because the State property is changed by the TextField without a call to withAnimation().

Is it possible to get this change animated?

6
  • 1
    What sort of animation would this be outside of SwiftUI? In other words, what "change" do you think is being animated here, and how? Commented Jun 8, 2019 at 20:11
  • What do you mean by outside of SwiftUI? In my example I want to fade the Button in and out, but I need this animation feature for more complex animations, which involve colors etc. But all the animations, which I need, would function normally, if the withAnimation() function was called. Commented Jun 8, 2019 at 20:14
  • Fade in\out the button or TextField? In your example you are trying to animate text that is being changed while the user tapping and it doesn't look like what you want Commented Jun 8, 2019 at 20:47
  • In example #1 there are two things, which happen, when I press the Button. Firstly the "Foo" Text is shown or hidden and secondly the text of the button is toggled between "Hide" and "Show". Both "changes" happen with the default animation, which is simply a fade animation. In example #2 the "Done" Button is shown as long as the text is not empty, otherwise it‘s hidden. This happens without an animation. But it‘s not important, which animations exactly happen in which example, the only thing of my interest is, that in example #2 there is no animation while in example #1 there are some. Commented Jun 8, 2019 at 20:54
  • But in the first example you have a withAnimation wrapping the state change. In the second example you don't. So it is unclear what you imagine is supposed to animate. Commented Jun 8, 2019 at 21:31

2 Answers 2

14

Just add the animation modifier to wrap your button

  var body: some View {
    VStack {
      TextField($text, placeholder: Text("Foo")) {
        print("editing ended")
      }
//      if !text.isEmpty {
        Button(action: {}) {
          Text("Done")
        }
        .background(text.isEmpty ? Color.red : Color.yellow )
         //.animation(.basic(duration: 1))
        .animation(Animation.default.speed(1))
      }
    }
  }
Sign up to request clarification or add additional context in comments.

9 Comments

Okay. I tried it, but unfortunately it does only fade in the Button but removes it immediately. Bug maybe? I tried it also with some modifier changes, for example .foregroundColor(text.isEmpty ? .red : .green) on the Button instead of the if, but that also doesn't animate.
My code should only fade in/out the button. The animation of foregroundColor really doesn't work. Try the same with background. Don't forget to put the changes before the animation modifier
Yeah I know, but it doesn’t fade out. Only in
Hmm. Pretty strange in/out works for me with this code
Yes I tried it now in Playground there it worked, but not for colors. In the Simulator it only works for fade in. I don’t know...
|
8

TextField("Placeholder", text:$text.animation())

Everything that uses the text will be animated when it is changed.

1 Comment

Worked even on Toggle!

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.