我想制作幻灯片右侧动画,就像Siri窗口一样。所以在用户点击按钮后,我这样做:
self.myWindowController.showWindow(nil)
在窗口控制器中:
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
}
但是窗口在1秒延迟后没有动画地移动到右上方位置。为什么?
答案 0 :(得分:2)
我认为使用不同的API来重置框架就可以了。
尝试做这样的事情:
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")
})
}
除非您正在绘制自己的绘图,否则您也无需显式设置animationBehavior
属性。拿出来。