呈现后立即取消初始化UIViewControllerTransitioningDelegate

时间:2019-02-22 00:48:08

标签: ios swift uiviewcontroller uipresentationcontroller

我正在尝试使用UIPresentationController(和UIViewControllerTransitioningDelegate)与自定义演示者以模态展示视图控制器。

问题是正在调用animationController(presented:presenting:source:)之后立即初始化过渡委托。这意味着animationController(dismissed:)从未被调用-因此无法设置解雇动画

最后,我希望能够定义解雇动画。我相信我在上面解释的是问题的根源,但是在网上找不到任何有关此的信息。


这是我对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
    }
}

这就是我设置过渡委托的方式:

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

然后关闭视图控制器时,视图始终默认为被按下并消失。同样,animationController(dismissed:)从未被调用过,我也不知道为什么。

1 个答案:

答案 0 :(得分:0)

我能够通过在当前视图控制器上存储对UIViewControllerTransitioningDelegate的引用来解决此问题。然后,在显示模态时,如下设置:

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