Bird
Raised Fist0
iOS Swiftmobile~20 mins

matchedGeometryEffect in iOS Swift - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of matchedGeometryEffect in SwiftUI?
easy
A. To manage data persistence
B. To create a new view from scratch
C. To handle user input events
D. To link two views for smooth animations between them

Solution

  1. Step 1: Understand matchedGeometryEffect usage

    This modifier connects two views by their id and namespace to animate changes smoothly.
  2. Step 2: Identify the purpose

    It is used specifically for animating transitions in position, size, or shape between views.
  3. Final Answer:

    To link two views for smooth animations between them -> Option D
  4. Quick Check:

    matchedGeometryEffect = link views for animation [OK]
Hint: matchedGeometryEffect connects views for smooth animations [OK]
Common Mistakes:
  • Thinking it creates new views
  • Confusing it with event handling
  • Assuming it manages data
2. Which of the following is the correct way to apply matchedGeometryEffect to a view in SwiftUI?
easy
A. view.matchedGeometryEffect("uniqueID", ns)
B. view.matchedGeometryEffect(id: "uniqueID", namespace: ns)
C. view.matchedGeometryEffect(id: ns, namespace: "uniqueID")
D. view.matchedGeometryEffect(namespace: "uniqueID", id: ns)

Solution

  1. Step 1: Recall matchedGeometryEffect syntax

    The correct syntax requires named parameters: id as a unique identifier and namespace as the shared namespace.
  2. Step 2: Match parameters correctly

    view.matchedGeometryEffect(id: "uniqueID", namespace: ns) uses id: "uniqueID" and namespace: ns, which is correct.
  3. Final Answer:

    view.matchedGeometryEffect(id: "uniqueID", namespace: ns) -> Option B
  4. Quick Check:

    Correct syntax uses named parameters id and namespace [OK]
Hint: Use named parameters id and namespace in matchedGeometryEffect [OK]
Common Mistakes:
  • Swapping id and namespace parameters
  • Omitting parameter names
  • Passing parameters in wrong order
3. Consider this SwiftUI code snippet:
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?
medium
A. The rectangle smoothly animates between small and large sizes
B. The rectangle instantly changes size without animation
C. The app crashes due to namespace error
D. The button does not toggle the rectangle size

Solution

  1. Step 1: Understand matchedGeometryEffect behavior

    Using the same id and namespace links the two rectangles for animation.
  2. Step 2: Analyze the toggle effect

    Toggling isExpanded switches between two frames, triggering a smooth size animation.
  3. Final Answer:

    The rectangle smoothly animates between small and large sizes -> Option A
  4. Quick Check:

    matchedGeometryEffect animates size changes smoothly [OK]
Hint: Same id and namespace animate size changes smoothly [OK]
Common Mistakes:
  • Expecting instant size change without animation
  • Confusing namespace usage
  • Thinking toggle won't affect view
4. Given this code snippet, what is the error that prevents the matchedGeometryEffect animation from working?
@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)
    }
  }
}
medium
A. Namespace is not declared properly
B. Frames are missing for the views
C. The ids for matchedGeometryEffect are different, so no animation occurs
D. showSquare is not toggled anywhere

Solution

  1. Step 1: Check matchedGeometryEffect ids

    The Circle uses id "circle" and the Rectangle uses id "square"; they must share the same id to animate.
  2. Step 2: Understand animation linking

    Different ids mean views are not linked, so no smooth animation happens.
  3. Final Answer:

    The ids for matchedGeometryEffect are different, so no animation occurs -> Option C
  4. Quick Check:

    Same id needed for matchedGeometryEffect animation [OK]
Hint: Use same id for views to animate with matchedGeometryEffect [OK]
Common Mistakes:
  • Assuming different ids animate
  • Ignoring namespace importance
  • Missing frame sizes
5. You want to animate a card flipping effect between a front and back view using matchedGeometryEffect. Which approach correctly achieves this?
hard
A. Use the same id and namespace on front and back views, toggle visibility, and animate rotation
B. Use different id values for front and back views with the same namespace
C. Apply matchedGeometryEffect only to the front view and toggle the back view without it
D. Use matchedGeometryEffect with no namespace to link the views

Solution

  1. Step 1: Understand matchedGeometryEffect for flipping

    To animate a flip, both front and back views must share the same id and namespace to link their geometry.
  2. Step 2: Combine with rotation animation

    Toggling visibility and animating rotation creates the flip effect smoothly.
  3. Final Answer:

    Use the same id and namespace on front and back views, toggle visibility, and animate rotation -> Option A
  4. Quick Check:

    Same id + namespace + rotation = flip animation [OK]
Hint: Flip needs same id, namespace, and rotation animation [OK]
Common Mistakes:
  • Using different ids breaks animation
  • Forgetting to animate rotation
  • Omitting namespace causes errors
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard