简体   繁体   中英

Presenting View controller disappears after animation

I've a TabBarController in which presents a view controller with custom animations with UIViewControllerAnimatedTransitioning;

The animations run normally without issues, but after the animationController(forPresented) function runs, the Presenting view controller disappears.

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.

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. And if you want to see background underneath, just set the contatiner's view background color to .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.

Anyone using transmission animation read the documentation, which was updated and you'll find really interesting and solid explanations there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM