简体   繁体   English

呈现后立即取消初始化UIViewControllerTransitioningDelegate

[英]UIViewControllerTransitioningDelegate being deinitialized immediately after it is presented

I'm trying to present a view controller modally with a custom presenter using UIPresentationController (and UIViewControllerTransitioningDelegate ). 我试图使用UIPresentationController (和UIViewControllerTransitioningDelegate )与自定义演示者以模态展示视图控制器。

The problem is that the transitioning delegate is being deinitialized immediately after animationController(presented:presenting:source:) is called. 问题在于, 在调用animationController(presented:presenting:source:)之后立即将过渡委托进行初始化。 This means animationController(dismissed:) never gets called - and thus, a dismissal animation cannot be set. 这意味着无法调用animationController(dismissed:) -因此无法设置解雇动画。

In the end, I want to be able to define the dismissal animation. 最后,我希望能够定义解雇动画。 I believe what I explained above is the root of the problem, but can't find anything about this online. 我相信我上面所解释的是问题的根源,但是在网上找不到任何有关此的信息。


Here is my implementation of UIViewControllerTransitioningDelegate : 这是我的UIViewControllerTransitioningDelegate实现:

final class Manager: NSObject, UIViewControllerTransitioningDelegate {
    private let size: CGSize
    var animator: Animator

    init(size: CGSize) {
        self.size = size
        self.animator = Animator(duration: 0.4, loaf: loaf)
    }

    deinit {
        print("DEINIT") // 2) Then this is being called immediately after
    }

    func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {
        return Controller(
            presentedViewController: presented,
            presenting: presenting,
            size: size
        )
    }

    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        animator.presenting = true
        return animator // 1) This is called first after the view controller is presented
    }

    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        animator.presenting = false
        return animator // 3) This is never called
    }
}

And this is how I'm setting the transitioning delegate: 这就是我设置过渡委托的方式:

extension UIViewController {
    func presentModally(_ viewController: UIViewController, size: CGSize) {
        viewController.transitioningDelegate = Manager(size: size)
        viewController.modalPresentationStyle = .custom
        present(viewController, animated: true)
    }
}

When the view controller is then dismissed, the view always defaults to being pushed down and disappearing. 然后关闭视图控制器时,视图始终默认为被按下并消失。 Again, animationController(dismissed:) is never called and I can't figure out why. 同样, animationController(dismissed:)从未被调用过,我也不知道为什么。

I was able to fix this by storing a reference to the UIViewControllerTransitioningDelegate on the presenting view controller. 我能够通过在当前视图控制器上存储对UIViewControllerTransitioningDelegate的引用来解决此问题。 Then, when presenting the modal, set it like this: 然后,在呈现模态时,应像这样设置它:

extension UIViewController {
    func presentModally(_ viewController: UIViewController, size: CGSize) {
        viewController.transDelegate = Manager(size: size)
        viewController.transitioningDelegate = viewController.transDelegate
        viewController.modalPresentationStyle = .custom
        present(viewController, animated: true)
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM