我正在为从初始屏幕呈现 UINavigationController
进行自定义转换,我应该只为 Root View Controller 设置动画。我有一个 UIStackView
里面有按钮。每个按钮和 UIStackView
都有一个阴影和层的属性 masksToBounds
设置为 false。我为 UINavigationController
编写了一个自定义动画器,我在 animateTransition(using transitionContext: UIViewControllerContextTransitioning)
方法中制作了所有动画。当我为现有的 UIStackView
设置动画时,我将它放入 transitionContext
的 containerView
中,当其他 UIViewController时它没有消失
已被推送。如果我使用 removeFromSuperView
,它也会从我的 rootViewController
中删除。我试图像这样复制 UIStackView
:
let archive = NSKeyedArchiver.archivedData(withRootObject: toVC.buttonsStackView)
let buttonsStackViewCopy = NSKeyedUnarchiver.unarchiveObject(with: archive) as! UIStackView
但它不会复制图层的属性、字体等。
我还尝试像这样将图层渲染到 ImageContext 中:
UIGraphicsBeginImageContext(toVC.buttonsStackView.bounds.size)
let context = UIGraphicsGetCurrentContext()!
toVC.buttonsStackView.layer.render(in: context)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
let buttonsStackViewSnapshot = UIImageView(image: image)
第一个问题是像使用 UIView
的简单快照一样剪切阴影。第二个问题是当我试图增加上下文大小时。阴影一切正常,但我无法计算正确的 UIImageView.frame
来制作正确的动画。
是否有任何解决方案如何复制 UIView
及其层或如何从 transitionContext.containerView
中删除它而不完全删除它?或者可能是我做错了所有事情?
最佳答案
事实上,复制 View 或其层是错误的想法。我花了两天时间解决这个问题,但今天我只是将我的 buttonsStackView.layer
作为子层添加到 containerView.layer
。然后,在 animate 方法的完成关闭中,我将它从 superLayer 中删除并添加到我的 rootViewController.view.layer
。效果很好。
transitionContext.containerView.layer.addSublayer(buttonsStackViewSnapshot)
UIView.animate(withDuration: duration,
animations: {
buttonsStackViewSnapshot.frame = finalButtonsFrame
},
completion: { (finished) in
buttonsStackViewSnapshot.removeFromSuperlayer()
toVC.view.layer.addSublayer(buttonsStackViewSnapshot)
toVC.view.isHidden = false
transitionContext.completeTransition(finished)
})
关于ios - 如何制作具有所有图层属性的 UIView 副本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48763402/