gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 16:20:59 27 4
gpt4 key购买 nike

我一直在尝试在 NSPopover 内创建从一个 subview Controller 到另一个 subview Controller 的滑动过渡。

我的问题是 subview Controller 在转换期间不会粘在 NSPopover 的顶部。它们从底部或顶部开始动画:

Demo GIF

预期行为:两个 subview Controller 在转换期间都应粘在顶部,并且应简单地水平滑动。

这是我编写的用于触发转换的函数:

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)
})
}

知道是什么原因导致了这个问题吗?我尝试过处理 subview Controller 和父 View Controller 及其帧大小的约束。我就是不明白我做错了什么。

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

非常感谢您的帮助!

最佳答案

如果您期望如下面的动画所示

demo

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

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()
}

更新:保持弹出内容可动画(不需要进行上述更改)

demo2

对于这种情况,您唯一需要的是翻转弹出内容 View (这是 MasterViewController 的 View )的坐标系

class PopoverContentView: NSView {
override var isFlipped: Bool { true }
}

class MasterViewController: NSViewController {
...
override func loadView() {
self.view = PopoverContentView()
}

关于objective-c - 如何在 NSViewController.transition 期间将 NSViewController 固定到 NSPopover 的顶部?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60460842/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com