无动画的自定义视图控制器演示文稿
问题描述:
我有一些自定义模态演示文稿和自定义控制器来呈现(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()!)
}
}
它的工作原理,但我不知道如果这是最好的办法。
文档说明,演示文稿控制器应仅负责在转换过程中执行任何其他设置或动画,并且演示的主要工作应在动画过渡对象中完成。
可以始终在演示文稿控制器中设置视图,而只在动画过渡对象中设置动画效果?
有没有更好的方法来解决这个问题?
答
通过将动画过渡的所有逻辑视图移动到表示控制器来解决这个问题。