---
title: "animate(changes:completion:)"
framework: swiftui
role: symbol
role_heading: Instance Method
path: "swiftui/nsviewrepresentablecontext/animate(changes:completion:)"
---

# animate(changes:completion:)

Animates changes using the animation in the current transaction.

## Declaration

```swift
@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

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(_:_:) 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()             }         }     } }
