<!--
{
  "availability" : [
    "iOS: 18.0.0 -",
    "iPadOS: 18.0.0 -",
    "macCatalyst: 18.0.0 -",
    "tvOS: 18.0.0 -",
    "visionOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "SwiftUI",
  "identifier" : "/documentation/SwiftUI/UIViewControllerRepresentableContext/animate(changes:completion:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "SwiftUI"
    ],
    "preciseIdentifier" : "s:7SwiftUI36UIViewControllerRepresentableContextV7animate7changes10completionyyyXE_yycSgtF"
  },
  "title" : "animate(changes:completion:)"
}
-->

# animate(changes:completion:)

Animates changes using the animation in the current transaction.

```
@MainActor @preconcurrency func animate(changes: () -> Void, completion: (() -> Void)? = nil)
```

## Parameters

`changes`

A closure that changes animatable properties.

`completion`

A closure to execute after the animation completes.

## Discussion

This combines
<doc://com.apple.documentation/documentation/UIKit/UIView/4429628-animate> with the
with the current transaction’s animation. When you start a SwiftUI
animation using [`withAnimation(_:_:)`](/documentation/SwiftUI/withAnimation(_:_:)) and have a mutated
SwiftUI state that causes the representable object to update, use
this method to animate changes in the representable object using the
same `Animation` timing.

```
struct ContentView: View {
    @State private var isCollapsed = false
    var body: some View {
        ZStack {
            MyDetailView(isCollapsed: isCollapsed)
            MyRepresentable(isCollapsed: $isCollapsed)
            Button("Collapse Content") {
                withAnimation(.bouncy) {
                    isCollapsed = true
                }
            }
        }
    }
}

struct MyRepresentable: UIViewControllerRepresentable {
    @Binding var isCollapsed: Bool

    func updateUIViewController(_ uiViewController: UIViewControllerType, context: Context) {
        if isCollapsed && !uiViewController.view.isCollapsed {
            context.animate {
                uiViewController.collapseSubview()
                uiView.layout()
            }
        }
    }
}
```

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)