0

我正在尝试使用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:)从未被调用,我不知道为什么。

4

1 回答 1

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)
    }
}
于 2019-02-22T02:33:39.327 回答