3

This is a simplified representation of my code. How do I add an animation to Text("hi") here? It's a really simple appearance animation.

import SwiftUI

struct Test: View {
    @State var showText:Bool = true
    var body: some View {
        List{
            HStack{
                Image(systemName: "square")
                Text("tap")
            }
            .onTapGesture {
                showText.toggle()
            }
            if (showText) {
                Text("Hi")
            }
        }
    }
}

struct Test_Previews: PreviewProvider {
    static var previews: some View {
        Test()
    }
}

appear disappear

1 Answer 1

2

You can use withAnimation block:

.onTapGesture {
    withAnimation {
        showText.toggle()
    }
}

You can also change the default animation type, duration etc:

withAnimation(.easeInOut(duration: 0.4)) { ... }
Sign up to request clarification or add additional context in comments.

1 Comment

Solved very well!!

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.