自定义过渡到存在视图控制器不起作用



在我的应用程序中,我想滑入和输出ViewController。因此,我正在使用自定义过渡,但应用程序崩溃了。我无法找出确切的问题是什么。请帮助我。

代码

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

我如何使用

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)

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

由于iOS 13中的Apple的API变化,因此。我面临同样的问题添加这一行解决了我的问题。

vc.modalpresentationstyle = .fullscreen

最新更新