显示视图控制器的自定义转换不起作用

Son*_*war 5 animation presentmodalviewcontroller ios

在我的应用程序中,我想滑入和滑出视图控制器。因此,为此我正在使用自定义过渡,但应用程序崩溃了。我无法找出确切的问题所在。请帮帮我。

代码

class CustomTransition: NSObject, UIViewControllerAnimatedTransitioning,UIViewControllerTransitioningDelegate {

    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {

        // get reference to our fromView, toView and the container view that we should perform the transition in
        let container = transitionContext.containerView
        let fromView = transitionContext.view(forKey: UITransitionContextViewKey.from)! // the app crashes here saying nil founded
        let toView = transitionContext.view(forKey: UITransitionContextViewKey.to)!
        let animationDuration = self .transitionDuration(using: transitionContext)
        let offScreenRight = CGAffineTransform(translationX: container.frame.width, y: 0)
        let offScreenLeft = CGAffineTransform(translationX: -container.frame.width, y: 0)

        toView.transform = offScreenRight

        // add the both views to our view controller
        container.addSubview(toView)
        container.addSubview(fromView)

        UIView.animate(withDuration: animationDuration, delay: 0.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.8, options: [] , animations: {
            fromView.transform = offScreenLeft
            toView.transform = CGAffineTransform.identity
        }, completion: { finished in
            // tell our transitionContext object that we've finished animating
            transitionContext.completeTransition(true)
        })
    }

    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        return 1
    }

    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return self
    }

    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return self
    }
}
Run Code Online (Sandbox Code Playgroud)

我如何使用

let vc = self.getViewControllerFromStoryBoard(storyBoardName: FORGOT_PASSWORD_SB, identifier: FORGOT_PASSWORD_IDENTIFIER) as! ForgotPassword
let a = CustomTransition()
vc.transitioningDelegate = a
vc.modalPresentationStyle = .custom
self.present(vc, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)

该应用程序崩溃,let fromView = transitionContext.view(forKey: UITransitionContextViewKey.from)!说 nil 成立。我错过了什么吗?

Kru*_*dia 0

由于 iOS 13 更新中 Apple 的 API 发生变化。我遇到了同样的问题添加这一行解决了我的问题。

vc.modalPresentationStyle = .fullScreen