【发布时间】:2026-01-18 16:45:01
【问题描述】:
我正在尝试使用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:) 从未被调用,我不知道为什么。
【问题讨论】:
-
你需要一个强引用,因为 transitioningDelegate 属性很弱。
-
@maddy 强引用将保存在哪里?
标签: ios swift uiviewcontroller uipresentationcontroller