Challenge - 5 Problems
matchedGeometryEffect Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2:00remaining
What happens when matchedGeometryEffect IDs don't match?
Consider two views using matchedGeometryEffect with different IDs. What is the visible effect during animation?
iOS Swift
struct ContentView: View {
@State private var toggle = false
@Namespace private var ns
var body: some View {
VStack {
if toggle {
Circle().matchedGeometryEffect(id: "circle1", in: ns)
.frame(width: 100, height: 100)
} else {
Circle().matchedGeometryEffect(id: "circle2", in: ns)
.frame(width: 50, height: 50)
}
}.onTapGesture { toggle.toggle() }
}
}Attempts:
2 left
💡 Hint
Think about how matchedGeometryEffect links views by their IDs.
✗ Incorrect
matchedGeometryEffect requires the same ID and namespace to animate smoothly between views. Different IDs mean no link, so no animation happens.
❓ lifecycle
intermediate2:00remaining
When is the matchedGeometryEffect animation triggered?
In SwiftUI, matchedGeometryEffect animates changes between views. Which user action triggers this animation in the example below?
iOS Swift
struct ContentView: View {
@Namespace private var ns
@State private var show = false
var body: some View {
VStack {
if show {
RoundedRectangle(cornerRadius: 25).matchedGeometryEffect(id: "box", in: ns)
.frame(width: 200, height: 200)
} else {
RoundedRectangle(cornerRadius: 25).matchedGeometryEffect(id: "box", in: ns)
.frame(width: 100, height: 100)
}
Button("Toggle") { show.toggle() }
}
}
}Attempts:
2 left
💡 Hint
Look at what changes the @State variable controlling the views.
✗ Incorrect
The animation happens when the "show" state toggles, causing the view to switch between two sizes linked by matchedGeometryEffect.
📝 Syntax
advanced2:00remaining
Identify the syntax error in matchedGeometryEffect usage
Which option contains a syntax error in using matchedGeometryEffect in SwiftUI?
Attempts:
2 left
💡 Hint
Check the parameters matchedGeometryEffect accepts.
✗ Incorrect
The 'properties' parameter does not exist in matchedGeometryEffect, causing a syntax error.
advanced
2:00remaining
How does matchedGeometryEffect behave across navigation links?
Given two views connected by a NavigationLink, both using matchedGeometryEffect with the same ID and namespace, what is the animation behavior when navigating?
iOS Swift
struct ListView: View {
@Namespace var ns
var body: some View {
NavigationView {
NavigationLink(destination: DetailView(ns: ns)) {
RoundedRectangle(cornerRadius: 10).matchedGeometryEffect(id: "shape", in: ns)
.frame(width: 50, height: 50)
}
}
}
}
struct DetailView: View {
var ns: Namespace.ID
var body: some View {
RoundedRectangle(cornerRadius: 10).matchedGeometryEffect(id: "shape", in: ns)
.frame(width: 200, height: 200)
}
}Attempts:
2 left
💡 Hint
Think about how matchedGeometryEffect links views even across navigation.
✗ Incorrect
matchedGeometryEffect can animate views between different screens if they share the same ID and namespace, enabling smooth transitions.
🔧 Debug
expert3:00remaining
Why does matchedGeometryEffect animation flicker unexpectedly?
A developer uses matchedGeometryEffect to animate a view between two states. Sometimes the animation flickers or jumps instead of smooth transition. What is the most likely cause?
iOS Swift
struct ContentView: View {
@Namespace var ns
@State var show = false
var body: some View {
VStack {
if show {
RoundedRectangle(cornerRadius: 20).matchedGeometryEffect(id: "box", in: ns)
.frame(width: 150, height: 150)
.transition(.opacity)
} else {
RoundedRectangle(cornerRadius: 20).matchedGeometryEffect(id: "box", in: ns)
.frame(width: 100, height: 100)
.transition(.opacity)
}
Button("Toggle") { show.toggle() }
}
}
}Attempts:
2 left
💡 Hint
Consider how transitions interact with matchedGeometryEffect animations.
✗ Incorrect
Applying .transition(.opacity) causes the view to fade out and fade in separately, interrupting the smooth geometry animation and causing flicker.