The matchedGeometryEffect helps you smoothly move or transform a view from one place to another in your app. It makes animations look natural and connected.
matchedGeometryEffect in iOS Swift
view.matchedGeometryEffect(id: "uniqueID", in: namespace)
The id is a unique string or value that links two views together.
The namespace is a special container that groups matched views for animation.
Text("Hello") .matchedGeometryEffect(id: "text", in: namespace)
Circle() .frame(width: 50, height: 50) .matchedGeometryEffect(id: "circle", in: namespace)
This example shows a blue rounded rectangle that grows from a small square to a large square when tapped. The matchedGeometryEffect links the two shapes so the animation is smooth and connected.
import SwiftUI struct ContentView: View { @Namespace private var namespace @State private var isExpanded = false var body: some View { VStack { if !isExpanded { RoundedRectangle(cornerRadius: 25) .fill(Color.blue) .frame(width: 100, height: 100) .matchedGeometryEffect(id: "shape", in: namespace) .onTapGesture { withAnimation(.spring()) { isExpanded = true } } } else { RoundedRectangle(cornerRadius: 25) .fill(Color.blue) .frame(width: 300, height: 300) .matchedGeometryEffect(id: "shape", in: namespace) .onTapGesture { withAnimation(.spring()) { isExpanded = false } } } } } }
Always use the same id and namespace for views you want to animate between.
The views must be in the same view hierarchy or visible at the same time for the effect to work.
You can animate many properties like size, position, and shape with this effect.
matchedGeometryEffect links two views for smooth animations.
Use a unique id and a shared namespace to connect views.
Great for animating changes in size, position, or shape between views.