'SwiftUI: Animate subview when map annotation is selected

I have a MapView implementing UIViewRepresentable with the following coord function:

        func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
        let ann = view.annotation
        mapView.setCenter(CLLocationCoordinate2D(latitude: ann!.coordinate.latitude+0.01, longitude: ann!.coordinate.longitude), animated: true)
        withAnimation {
            self.parent.stateMap.annotationIsSelected = true
        }
    }

Note: stateMap is an EnvironmentObject

I wish to animate a Subview triggered by the map annotation selection ie when stateMap.annotationIsSelected is set to true.

struct SubView: View {
    @EnvironmentObject var stateMap: StateMap
    var body: some View {
        if stateMap.annotationIsSelected {
            VStack(alignment: .leading) {
                Text("\(stateMap.destination?.name ?? "")").font(.headline)
            }.animation(.easeInOut, value: self.stateMap.annotationIsSelected)
        }
    }
}

However the subview does not animate at all. What I am getting wrong?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source