【问题标题】:Presenting View controller disappears after animation动画后呈现视图控制器消失
【发布时间】:2017-03-13 23:02:54
【问题描述】:

我有一个 TabBarController,其中显示了一个带有 UIViewControllerAnimatedTransitioning 的自定义动画的视图控制器;

动画正常运行没有问题,但是animationController(forPresented)函数运行后,Presenting view controller消失了。

我在这里发现了一个问题,人们有同样的问题,但这些尝试都没有解决我的问题。

我读到 iOS 中存在一个错误,我们应该再次将“消失”的视图控制器添加到堆栈中,但是将其添加到 UIApplication.shared.keyWindow?.addSubview(presentingView) 会使视图添加到 presentedView 之上,而我没有'不知道它再次添加它,将另一个添加到堆栈中,因为它可能只是一个图形错误并且视图仍然是容器的一部分。

这里有一些代码:

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

请注意,动画本身只是我正在做的测试,只是缩放它们。

谢谢。

【问题讨论】:

  • 我不知道的一件事是,我应该在添加时将presentingView 和presentedView 都添加到容器中,还是应该只添加presentingView。我真的迷失了这个
  • Apple 文档说:对于演示文稿,将“to”视图添加到容器视图层次结构中。对于解雇,从容器视图层次结构中删除“来自”视图。我认为我只需要将“to”视图添加到堆栈中

标签: ios animation swift3 uianimation


【解决方案1】:

看了苹果的相关文档here后发现presentingViewController从屏幕上消失不是bug,而是API的工作原理。

任何使用传输动画的人都可以阅读文档,该文档已更新,您会在其中找到非常有趣和扎实的解释。

【讨论】:

  • 我认为你应该在这里解释哪种解释是有帮助的。您在何时添加以及何时不将“to”视图添加到容器的问题下写了一条评论。该评论实际上是正确答案。
  • @pjuzeliunas 你是对的。自从我写这篇文章以来已经有一段时间了,并且在整个过程中完全忘记了
猜你喜欢
  • 1970-01-01
  • 2015-08-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多