ios - 呈现 View Controller 的自定义转换不起作用

标签 ios animation presentmodalviewcontroller

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

代码

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

关于ios - 呈现 View Controller 的自定义转换不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42553787/

相关文章:

iphone - 如何检测 NSURLConnection sendAsynchronousRequest 是否完成

ios - 带有数组错误的 Swift NSCountedSet 初始化?

javascript - 如何使用 JavaScript 为加载 CSS 的 SVG 背景设置动画

java - 可扩展组件以隐藏或取消隐藏另一个组件

ios - iPad presentModalViewController 出现在错误的位置

ios - 使用 iOS 8 SDK 在 iOS 9 设备上运行调试构建

ios - 如何返回零 CGFloat?

iphone - 您可以在 iPhone 中使用 Quartz 制作渐变动画吗?

SwiftUI:如何正确地以模态方式呈现 AVPlayerViewController?

ios - DismissViewController 关闭两个 UIViewController