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.
Practice
matchedGeometryEffect in SwiftUI?Solution
Step 1: Understand matchedGeometryEffect usage
This modifier connects two views by theiridandnamespaceto animate changes smoothly.Step 2: Identify the purpose
It is used specifically for animating transitions in position, size, or shape between views.Final Answer:
To link two views for smooth animations between them -> Option DQuick Check:
matchedGeometryEffect = link views for animation [OK]
- Thinking it creates new views
- Confusing it with event handling
- Assuming it manages data
matchedGeometryEffect to a view in SwiftUI?Solution
Step 1: Recall matchedGeometryEffect syntax
The correct syntax requires named parameters:idas a unique identifier andnamespaceas the shared namespace.Step 2: Match parameters correctly
view.matchedGeometryEffect(id: "uniqueID", namespace: ns) usesid: "uniqueID"andnamespace: ns, which is correct.Final Answer:
view.matchedGeometryEffect(id: "uniqueID", namespace: ns) -> Option BQuick Check:
Correct syntax uses named parameters id and namespace [OK]
- Swapping id and namespace parameters
- Omitting parameter names
- Passing parameters in wrong order
struct ContentView: View {
@Namespace var ns
@State var isExpanded = false
var body: some View {
VStack {
if isExpanded {
RoundedRectangle(cornerRadius: 25)
.matchedGeometryEffect(id: "shape", namespace: ns)
.frame(width: 300, height: 200)
} else {
RoundedRectangle(cornerRadius: 25)
.matchedGeometryEffect(id: "shape", namespace: ns)
.frame(width: 100, height: 100)
}
Button("Toggle") { isExpanded.toggle() }
}
}
}
What happens when the button is tapped?Solution
Step 1: Understand matchedGeometryEffect behavior
Using the sameidandnamespacelinks the two rectangles for animation.Step 2: Analyze the toggle effect
TogglingisExpandedswitches between two frames, triggering a smooth size animation.Final Answer:
The rectangle smoothly animates between small and large sizes -> Option AQuick Check:
matchedGeometryEffect animates size changes smoothly [OK]
- Expecting instant size change without animation
- Confusing namespace usage
- Thinking toggle won't affect view
@Namespace var ns
var body: some View {
VStack {
Circle()
.matchedGeometryEffect(id: "circle", namespace: ns)
.frame(width: 50, height: 50)
if showSquare {
Rectangle()
.matchedGeometryEffect(id: "square", namespace: ns)
.frame(width: 100, height: 100)
}
}
}Solution
Step 1: Check matchedGeometryEffect ids
The Circle uses id "circle" and the Rectangle uses id "square"; they must share the same id to animate.Step 2: Understand animation linking
Different ids mean views are not linked, so no smooth animation happens.Final Answer:
The ids for matchedGeometryEffect are different, so no animation occurs -> Option CQuick Check:
Same id needed for matchedGeometryEffect animation [OK]
- Assuming different ids animate
- Ignoring namespace importance
- Missing frame sizes
matchedGeometryEffect. Which approach correctly achieves this?Solution
Step 1: Understand matchedGeometryEffect for flipping
To animate a flip, both front and back views must share the sameidandnamespaceto link their geometry.Step 2: Combine with rotation animation
Toggling visibility and animating rotation creates the flip effect smoothly.Final Answer:
Use the sameidandnamespaceon front and back views, toggle visibility, and animate rotation -> Option AQuick Check:
Same id + namespace + rotation = flip animation [OK]
- Using different ids breaks animation
- Forgetting to animate rotation
- Omitting namespace causes errors
Ready to prove it?
Pass the test and this pattern moves unknown
