Я пытаюсь представить контроллер представления модально с помощью собственного презентатора, используя 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:)
никогда не вызывается, и я не могу понять, почему.