如何在NSViewController.transition期间将NSViewController固定到NSPopover的顶部?

问题描述 投票:1回答:1

我一直试图在NSPopover内部创建从一个子视图控制器到另一个子视图控制器的滑动过渡。

我的问题是,子视图控制器在过渡期间不会停留在NSPopover的顶部。它们从底部或顶部进行动画处理:

Demo GIF

预期的行为:在过渡期间,两个子视图控制器都应停留在顶部,并且只需在水平方向上滑动即可。

这是我编写的触发转换的功能:

func loadViewController(_ childViewController: NSViewController, withTransition transitionOptions: NSViewController.TransitionOptions?) {
        addChild(childViewController)
        view.addSubview(childViewController.view)

        childViewController.view.layer?.borderColor = NSColor(calibratedRed: 0, green: 255, blue: 0, alpha: 1).cgColor
        childViewController.view.layer?.borderWidth = 2

        childViewController.view.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
        view.layout()

        let oldViewController = currentViewController
        currentViewController = childViewController

        oldViewController?.view.layer?.borderColor = NSColor(calibratedRed: 255, green: 0, blue: 0, alpha: 1).cgColor
        oldViewController?.view.layer?.borderWidth = 2

        if let oldViewController = oldViewController  {
            transition(from: oldViewController, to: currentViewController!, options: transitionOptions ?? .slideLeft, completionHandler: { [weak oldViewController] in
                oldViewController?.removeFromParent()
                oldViewController?.view.removeFromSuperview()
            })
        }

        NSAnimationContext.runAnimationGroup({ (context) -> Void in
            context.duration = 0.5
            context.allowsImplicitAnimation = true

            self.parentPopover?.contentSize = NSSize(width: childViewController.preferredContentSize.width, height: childViewController.preferredContentSize.height)
        })
    }

任何想法可能导致问题的原因?我尝试过处理子视图控制器和父视图控制器的约束及其框架大小。我只是不知道我在做什么错。

我已经在此处上传了完整的可复制示例:https://github.com/maximilianschmitt/DebugPopoverAnimation

非常感谢您的帮助!

objective-c swift macos cocoa appkit
1个回答
0
投票

如果您期望像下面的动画一样

“

然后仅从动画块中删除内容大小的更新,如下所示

self.parentPopover?.contentSize = NSSize(width: childViewController.preferredContentSize.width, height: childViewController.preferredContentSize.height)
NSAnimationContext.runAnimationGroup({ (context) -> Void in
    context.duration = 0.5
    context.allowsImplicitAnimation = true

}) {
    oldViewController?.removeFromParent()
    oldViewController?.view.removeFromSuperview()
}
© www.soinside.com 2019 - 2024. All rights reserved.