我正在进行自定义转换,以便从初始屏幕显示UINavigationController
,我应该只设置根视图控制器的动画。我有一个带有按钮的UIStackView
。每个按钮和UIStackView
都将阴影和图层的属性masksToBounds
设置为false。我为UINavigationController
编写了一个自定义动画师,我在方法animateTransition(using transitionContext: UIViewControllerContextTransitioning)
中进行了所有动画制作。当我为现有UIStackView
设置动画时,我将其放入containerView
的{{1}}中,当其他transitionContext
被推送时,它并没有消失。如果我使用UIViewController
,它也会从我的removeFromSuperView
中删除。我试图像这样复制rootViewController
:
UIStackView
但它并没有复制图层的属性,字体和其他内容。
此外,我尝试将图层渲染到ImageContext中,如下所示:
let archive = NSKeyedArchiver.archivedData(withRootObject: toVC.buttonsStackView)
let buttonsStackViewCopy = NSKeyedUnarchiver.unarchiveObject(with: archive) as! UIStackView
第一个问题是使用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
删除它而不删除它?或者可能是我一般都做错了什么?
答案 0 :(得分:1)
事实上,复制视图或它的图层是错误的想法。我花了两天时间来解决这个问题,但今天我刚刚将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)
})