动画后呈现视图控制器消失

时间:2017-03-13 23:02:55

标签: ios animation swift3 uianimation

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

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

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

我已经读过iOS中有一个错误,我们应该再次消失了#39;查看控制器到堆栈,但添加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
        }
    }

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

THX。

1 个答案:

答案 0 :(得分:-1)

在阅读了Apple的相关文档here后,我发现它并不是一个错误,即showsViewController从屏幕上消失,它只是API的工作方式。

任何使用传输动画的人都会阅读已更新的文档,并且您会在那里找到非常有趣且可靠的解释。

相关问题