简体   繁体   中英

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 ?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM