> For the complete documentation index, see [llms.txt](https://lochiwei.gitbook.io/ios/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://lochiwei.gitbook.io/ios/swiftui/shapes/matched-geometry-effect/animation.md).

# MGE ⟩ animation

[SwiftUI](/ios/swiftui.md) ⟩ [Drawing](/ios/swiftui/shapes.md) ⟩ [Matched Geometry Effect](/ios/swiftui/shapes/matched-geometry-effect.md) ⟩&#x20;

{% embed url="<https://youtu.be/QUrHFGrqrJg>" %}

{% tabs %}
{% tab title="💾 程式" %}
⬆️ 需要： [view + .frame()](/ios/swiftui/view/layout/frame/ext.md)

```swift
import SwiftUI
import PlaygroundSupport
import Extensions            // 🌀 View+
PlaygroundPage.current.setLiveView(ContentView())

struct ContentView: View {
    
    // ⭐️ for matched geometry effect (i.e. Magic Move)
    @Namespace private var ns
    
    @State private var flag: Bool = true
    
    var body: some View {
        VStack(spacing: 0) {
            HStack {
                viewA1    // ⭐️ source/target of matched geometry effect
                Spacer()
                squares
            }
            controls
        }
        .frame(width: 400)
        .border(.secondary)
    }
}

extension ContentView {
    
    /// controls
    var controls: some View {
        HStack(spacing: 0) {
            Group {
                // ⭐️ animation
                Toggle("Anime", isOn: $flag.animation(.easeInOut(duration: 4)))
                Toggle("No", isOn: $flag).tint(.pink)
            }
            .padding()
            .border(.secondary)
        }
    }
    
    /// ⭐️ viewA1
    @ViewBuilder var viewA1: some View {
        if flag {
            Color.green
                // ----------------------------------
                // ⭐️ source/target
                .matchedGeometryEffect(id: 1, in: ns)
                // ----------------------------------
                .frame(100)        // 🌀 View+
        }
    }
    
    /// ⭐️ viewA2
    @ViewBuilder var viewA2: some View {
        if !flag {
            Circle()
                .fill(Color.blue)
                // ----------------------------------
                // ⭐️ target/source
                .matchedGeometryEffect(id: 1, in: ns)
                // ----------------------------------
                .padding(2)
                .frame(50)        // 🌀 View+
                .border(.secondary)
        }
    }
    
    /// square
    var square: some View {
        Color.pink
            .frame(50)            // 🌀 View+
            .border(.secondary)
    }
    
    /// square stack
    var squares: some View {
        VStack(spacing: 0) {
            square
            viewA2        // ⭐️ target/source of matched geometry effect
            square
        }
    }
}
```

{% endtab %}

{% tab title="📗 參考" %}

* SwiftUI Lab ⟩ MatchedGeometryEffect ⟩ [Part 1 (Hero Animations)](https://swiftui-lab.com/matchedgeometryeffect-part1/)
  {% endtab %}

{% tab title="👥 相關" %}

* can have [animations](/ios/swiftui/anim.md).
  {% endtab %}
  {% endtabs %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://lochiwei.gitbook.io/ios/swiftui/shapes/matched-geometry-effect/animation.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
