我正在尝试使用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:)
,我也不知道为什么。 最佳答案
我能够通过在当前视图控制器上存储对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)
}
}