5

我有一些自定义模式演示和自定义控制器要演示(UIViewController 的子类)。它是它自己的过渡委托,并返回一些动画过渡对象和演示控制器。我使用动画过渡对象在呈现时将呈现的视图添加到容器视图中,并在关闭时将其移除,当然还有动画。我使用演示控制器添加一些辅助子视图。

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)
    }

}

当我向控制器展示并在属性presentViewController中传递 true时,一切正常。animated但是当我想在没有动画的情况下呈现它并传递 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()!)
    }
}

它有效,但我不确定这是否是最好的方法。

文档说,演示控制器应该只负责在过渡期间进行任何额外的设置或动画,并且演示的主要工作应该在动画过渡对象中完成。

是否可以始终在演示控制器中设置视图并仅在动画过渡对象中对其进行动画处理?

有没有更好的方法来解决这个问题?

4

1 回答 1

1

通过将视图设置的所有逻辑从动画转换移动到演示控制器来解决这个问题。

于 2017-03-09T19:48:55.473 回答