<!--
{
  "availability" : [
    "macOS: 15.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "SwiftUI",
  "identifier" : "/documentation/SwiftUI/NSViewRepresentableContext/animate(changes:completion:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "SwiftUI"
    ],
    "preciseIdentifier" : "s:7SwiftUI26NSViewRepresentableContextV7animate7changes10completionyyyXE_yycSgtF"
  },
  "title" : "animate(changes:completion:)"
}
-->

# animate(changes:completion:)

Animates changes using the animation in the current transaction.

```
@backDeployed(before: macOS 15.4)
@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/appkit/nsanimationcontext/4433144-animate>
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: NSViewRepresentable {
    @Binding var isCollapsed: Bool

    func updateNSView(_ nsView: NSViewType, context: Context) {
        if isCollapsed && !nsView.isCollapsed {
            context.animate {
                nsView.collapseSubview()
                nsView.layoutSubtreeIfNeeded()
            }
        }
    }
}
```

---

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)