我有一些自定義模態演示文稿和自定義控制器來呈現(UIViewController的子類)。它是它自己的轉換委託並返回一些動畫轉換對象和表示控制器。我使用動畫過渡對象在呈現時向容器視圖添加呈現的視圖,並在解散時將其移除,當然也可以爲其設置動畫效果。我使用演示文稿控制器添加一些輔助子視圖。當我提出的控制器presentViewController
和animated
財產傳給真正無動畫的自定義視圖控制器演示文稿
public final class PopoverPresentationController: UIPresentationController {
private let touchForwardingView = TouchForwardingView()
override public func presentationTransitionWillBegin() {
super.presentationTransitionWillBegin()
self.containerView?.insertSubview(touchForwardingView, atIndex: 0)
}
}
public final class PopoverAnimatedTransitioning: NSObject, UIViewControllerAnimatedTransitioning {
func setupView(containerView: UIView, presentedView: UIView) {
//adds presented view to container view
}
public func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
//1. setup views
//2. animate presentation or dismissal
}
}
public class PopoverViewController: UIViewController, UIViewControllerTransitioningDelegate {
init(...) {
...
modalPresentationStyle = .Custom
transitioningDelegate = self
}
public func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return PopoverAnimatedTransitioning(forPresenting: true, position: position, fromView: fromView)
}
public func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return PopoverAnimatedTransitioning(forPresenting: false, position: position, fromView: fromView)
}
public func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController?, sourceViewController source: UIViewController) -> UIPresentationController? {
return PopoverPresentationController(presentedViewController: presented, presentingViewController: presenting, position: position, fromView: fromView)
}
}
,一切工作正常。但是,如果我想在不使用動畫的情況下呈現它並傳遞false,則UIKit只會調用presentationControllerForPresentedViewController
方法,根本不會調用animationControllerForPresentedController
。並且,只要呈現的視圖添加到視圖層次結構中,並將其放置在動畫轉換對象中,而該對象從不創建,則不會顯示任何內容。
我在做什麼是我檢查演示文稿控制器如果過渡是動畫,如果不是我手動創建動畫過渡對象,並使其設置視圖。
override public func presentationTransitionWillBegin() {
...
if let transitionCoordinator = presentedViewController.transitionCoordinator() where !transitionCoordinator.isAnimated() {
let transition = PopoverAnimatedTransitioning(forPresenting: true, position: position, fromView: fromView)
transition.setupView(containerView!, presentedView: presentedView()!)
}
}
它的工作原理,但我不知道如果這是最好的辦法。
文檔說明,演示文稿控制器應僅負責在轉換過程中執行任何其他設置或動畫,並且演示的主要工作應在動畫過渡對象中完成。
可以始終在演示文稿控制器中設置視圖,而只在動畫過渡對象中設置動畫效果?
有沒有更好的方法來解決這個問題?