Animate a macOS window

(swift, macOS, storyboards)

I can animate a window just adding animate: true to a regular way to control a window. Is there any way to control the animation, especially the time the animation will take?

self.view.window?.setFrame(NSRect(x:0, y:0,
                                width:300, height:300),
                                display: true, animate: true)
Answered by Claude31 in 686218022

Use NSAnimation.

For instance, if you want to animate when clicking a button:

    @IBAction func startAnimation(_ sender: NSButton) {

        let fixedDuration = 5.0   // set duration as you want, in seconds
        NSAnimationContext.runAnimationGroup( { (context) in
            context.duration = fixedDuration 
            self.view.window?.animator().setFrame(CGRect(x: 0, y: 0, width: 200, height: 200), display: true, animate: true)
        }) 
    }
Accepted Answer

Use NSAnimation.

For instance, if you want to animate when clicking a button:

    @IBAction func startAnimation(_ sender: NSButton) {

        let fixedDuration = 5.0   // set duration as you want, in seconds
        NSAnimationContext.runAnimationGroup( { (context) in
            context.duration = fixedDuration 
            self.view.window?.animator().setFrame(CGRect(x: 0, y: 0, width: 200, height: 200), display: true, animate: true)
        }) 
    }

That is just fantastic!

I have been hours searching and trying things and you solved in a minute!. Thank you!

Animate a macOS window
 
 
Q