0Pricing
Swift Academy · Lesson

matchedGeometryEffect for Hero Transitions

Creating seamless hero animations between views with namespace and matched IDs.

What is matchedGeometryEffect?

matchedGeometryEffect animates a view's position and size between two states, creating a "hero" transition.

struct HeroDemo: View {
  @Namespace private var ns
  @State private var expanded = false
  var body: some View {
    if expanded {
      BigCard().matchedGeometryEffect(id: "card", in: ns)
    } else {
      SmallCard().matchedGeometryEffect(id: "card", in: ns)
    }
  }
}

Creating a Namespace

Declare a @Namespace in the parent view. All matched views share this namespace.

struct ParentView: View {
  @Namespace private var heroNamespace
}

All lessons in this course

  1. Implicit vs Explicit Animations
  2. matchedGeometryEffect for Hero Transitions
  3. Custom AnimatableModifier and Animatable
  4. TimelineView and Canvas for High-Performance Drawing
← Back to Swift Academy