JH.KIM
JH.KIM

Reputation: 47

SwiftUI add Animation

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

Upvotes: 3

Views: 121

Answers (1)

pawello2222
pawello2222

Reputation: 54466

You can use withAnimation block:

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

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

withAnimation(.easeInOut(duration: 0.4)) { ... }

Upvotes: 2

Related Questions