[英]Presenting View controller disappears after animation
I've a TabBarController in which presents a view controller with custom animations with UIViewControllerAnimatedTransitioning;我有一个 TabBarController,其中显示一个视图 controller,其中包含带有 UIViewControllerAnimatedTransitioning 的自定义动画;
The animations run normally without issues, but after the animationController(forPresented)
function runs, the Presenting view controller
disappears.动画正常运行没有问题,但在animationController(forPresented)
function 运行后, Presenting view controller
消失了。
I've found a question around here with people having the same issues but none of those tries solved my issue.我在这里发现了一个问题,人们有同样的问题,但这些尝试都没有解决我的问题。
I've read that there is a bug in iOS and we should had again the 'vanished' view controller to the stack, but adding this with UIApplication.shared.keyWindow?.addSubview(presentingView)
makes the view added on top of the presentedView
and I don't know it adding it again, adds another one to the stack, because it could only be a graphical bug and the view is still part of the container.我读到 iOS 中有一个错误,我们应该再次将“消失”视图 controller 添加到堆栈中,但是使用UIApplication.shared.keyWindow?.addSubview(presentingView)
添加它会使视图添加到presentedView
之上而且我不知道它再次添加它,将另一个添加到堆栈中,因为它可能只是一个图形错误并且视图仍然是容器的一部分。
Here's some code:这是一些代码:
// Global var
var transition = Animator()
// Present a VC modally using tab bar
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
if viewController is NewPostVC {
if let newVC = tabBarController.storyboard?.instantiateViewController(withIdentifier: "newPostVC") as? NewPostVC {
newVC.transitioningDelegate = self
newVC.interactor = interactor // new
tabBarController.present(newVC, animated: true)
return false
}
}
return true
}
// Handles the presenting animation
func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
transition.transitioningMode = .Present
return transition
}
// Handles the dismissing animation
func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
transition.transitioningMode = .Dismiss
return transition
}
// interaction controller, only for dismissing the view;
func interactionControllerForDismissal(using animator: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning? {
return interactor.hasStarted ? interactor : nil
}
//*****************************
/// On the Animator class:
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
let containerView = transitionContext.containerView
// Animates to present
if transitioningMode == .Present {
// Get views
guard
let presentedView = transitionContext.view(forKey: UITransitionContextViewKey.to),
let presentingView = transitionContext.view(forKey: UITransitionContextViewKey.from)
else {
print("returning")
return
}
// Add the presenting view controller to the container view
containerView.addSubview(presentingView)
// Add the presented view controller to the container view
containerView.addSubview(presentedView)
// Animate
UIView.animate(withDuration: presentDuration, animations: {
presentingView.transform = CGAffineTransform.identity.scaledBy(x: 0.85, y: 0.85);
presentingView.layer.cornerRadius = 7.0
presentingView.layer.masksToBounds = true
presentedView.transform = CGAffineTransform.identity.scaledBy(x: 0.6, y: 0.6);
presentedView.layer.masksToBounds = true
}, completion: { _ in
// On completion, complete the transition
transitionContext.completeTransition(true)
//UIApplication.shared.keyWindow?.addSubview(presentingView)
})
}
// Animates to dismiss
else {
// TODO: - Implement reverse animation
}
}
Note that the animations itself are just tests I'm doing, just scaling them around.请注意,动画本身只是我正在做的测试,只是缩放它们。
Thx.谢谢。
The idea is that you don't need to add subview .from
during presentation and .to
during dismissal to the contatiner's view hierarchy.这个想法是你不需要在演示期间添加子视图.from
和在解雇期间添加.to
到contatiner的视图层次结构。 And if you want to see background underneath, just set the contatiner's view background color to .clear
.如果你想看到下面的背景,只需将容器的视图背景颜色设置为.clear
。
After reading Apple's related documentation here I found that it's not a bug that the presentingViewController disappears from the screen, it's just how the API works. 在这里阅读了Apple的相关文档后,我发现presentingViewController从屏幕上消失并不是错误,而是API的工作方式。
Anyone using transmission animation read the documentation, which was updated and you'll find really interesting and solid explanations there. 任何使用传输动画的人都阅读了文档,该文档已更新,您将在其中找到真正有趣且扎实的说明。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.