Contents

animate(_:changes:completion:)

Animate changes to one or more views using the specified SwiftUI animation.

Declaration

static func animate(_ animation: Animation, changes: () -> Void, completion: (() -> Void)? = nil)

Parameters

  • animation:

    The animation to use for the changes.

  • changes:

    A closure containing the changes to animate.

  • completion:

    A closure to execute after the animation completes.

Discussion

Animations performed using this method can be smoothly retargeted while preserving velocity, just like animations in SwiftUI views.

// Grow the window with a smooth spring animation
NSAnimationContext.animate(.smooth) {
    var scaledFrame = myWindowContentView.frame.applying(CGAffineTransform(scaleX: 1.5, y: 1.5)
    myWindowContentView.setFrameSize(myScaledFrame)
}