I am making app which is using Matched Geometry Effect. It is matching two rectangles embedded inside two different views. It isn’t working as it supposed to. Please help me.
Here is test view for that:
struct ContentViewTest: View {
@State var details = false
@Namespace var animation
var body: some View {
ZStack {
if !details {
TestView2(details: $details, anim: animation)
}
if details {
TestView1(details: $details, anim: animation)
}
}
}
}
Here is TestView1:
struct TestView1: View {
@Binding var details: Bool
var anim: Namespace.ID
var body: some View {
ZStack{
Color.gray.ignoresSafeArea()
Color.red.frame(width: 300, height: 700)
.matchedGeometryEffect(id: "id1", in: anim)
.onTapGesture {
withAnimation {
details = false
}
}
}
}
}
And here is TestView2:
struct TestView2: View {
@Binding var details: Bool
var anim: Namespace.ID
var body: some View {
ZStack{
Color.green.ignoresSafeArea()
Color.red.frame(width: 200, height: 200)
.matchedGeometryEffect(id: "id1", in: anim)
.onTapGesture {
withAnimation {
details = true
}
}
}
}
}
2
Answers
This an approach for your issue, using transition with along matchedGeometryEffect in correct place:
Just add
properties
position (in both matched views), like in belowor (depending on effect which you want to achieve) change order of modifiers (for both views), like
Tested with Xcode 13.2 / iOS 15.2