Arti
Arti

Reputation: 7762

nswindow animator not working in window controller

I want to make slide right animation, like Siri window. So after user click button, i do:

self.myWindowController.showWindow(nil)

In window controller:

override func windowDidLoad() {
        super.windowDidLoad()

        if let window = window, let screen = window.screen {
            window.makeKeyAndOrderFront(nil)
            window.animationBehavior = .none

            let offsetFromLeftOfScreen: CGFloat = 30
            let offsetFromTopOfScreen: CGFloat = 30
            let screenRect = screen.visibleFrame
            let newOriginX = screenRect.maxX - window.frame.width - offsetFromLeftOfScreen
            let newOriginY = screenRect.maxY - window.frame.height - offsetFromTopOfScreen

            DispatchQueue.main.asyncAfter(deadline: .now()+1.0) {
                NSAnimationContext.beginGrouping()
                NSAnimationContext.current().duration = 3.0
                window.animator().setFrameOrigin(NSPoint(x: newOriginX, y: newOriginY))
                NSAnimationContext.endGrouping()
            }

            window.titleVisibility = .hidden
        }

But window move to top right position without animation after 1 second delay. Why ?

Upvotes: 0

Views: 285

Answers (1)

Michael Dautermann
Michael Dautermann

Reputation: 89509

I think using a different API to reset the frame will do the trick.

Try doing something like this:

    let newFrame = CGRect(x: newOriginX, y: newOriginY, width: window.frame.size.width, height: window.frame.size.height)
    DispatchQueue.main.asyncAfter(deadline: .now()+1.0) {
        NSAnimationContext.runAnimationGroup({ (context) in
            context.duration = 3.0

            window.animator().setFrame(newFrame, display: true, animate: true)
        }, completionHandler: {
            print("animation done")
        })
    }

You also do not need to explicitly set the animationBehavior property, unless you are doing your own drawing. Take that out.

Upvotes: 2

Related Questions