0
0
iOS Swiftmobile~20 mins

matchedGeometryEffect in iOS Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
matchedGeometryEffect Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2: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() }
  }
}
AThe circle fades out and a new circle fades in.
BThe circle smoothly animates between sizes and positions.
CThe app crashes due to mismatched IDs.
DNo animation occurs; the circle abruptly changes size and position.
Attempts:
2 left
💡 Hint
Think about how matchedGeometryEffect links views by their IDs.
lifecycle
intermediate
2: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() }
    }
  }
}
AWhen the "Toggle" button is pressed and the state changes.
BWhen the view appears for the first time.
CWhen the app launches.
DWhen the user taps the RoundedRectangle.
Attempts:
2 left
💡 Hint
Look at what changes the @State variable controlling the views.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in matchedGeometryEffect usage
Which option contains a syntax error in using matchedGeometryEffect in SwiftUI?
AImage(systemName: "star").matchedGeometryEffect(id: "star", in: ns, properties: .frame)
BRectangle().matchedGeometryEffect(id: "rect", in: ns, anchor: .center)
CCircle().matchedGeometryEffect(id: "circle", in: ns, isSource: true)
DText("Hello").matchedGeometryEffect(id: "text", in: ns)
Attempts:
2 left
💡 Hint
Check the parameters matchedGeometryEffect accepts.
navigation
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)
  }
}
ANo animation occurs because matchedGeometryEffect does not work across NavigationLinks.
BThe shape smoothly animates from small to large during navigation.
CThe app crashes due to namespace sharing.
DThe shape instantly changes size without animation.
Attempts:
2 left
💡 Hint
Think about how matchedGeometryEffect links views even across navigation.
🔧 Debug
expert
3: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() }
    }
  }
}
AThe matchedGeometryEffect IDs are different between views.
BThe namespace variable is not marked @Namespace, causing animation failure.
CUsing .transition(.opacity) causes the view to fade out and in, breaking the geometry animation.
DThe RoundedRectangle shapes have different cornerRadius values.
Attempts:
2 left
💡 Hint
Consider how transitions interact with matchedGeometryEffect animations.