自定义过渡以显示视图控制器不起作用

时间:2017-03-02 11:08:37

标签: ios animation presentmodalviewcontroller

在我的应用程序中,我想要滑入和滑出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成立了。 我错过了什么吗?

1 个答案:

答案 0 :(得分:0)

由于Apple在iOS 13中对API进行了更改,因此进行了更新。我遇到了同样的问题添加此行解决了我的问题。

  

vc.modalPresentationStyle = .fullScreen