自定义视图控制器演示无动画

时间:2016-11-12 16:34:46

标签: ios uikit uiviewanimationtransition presentviewcontroller uipresentationcontroller

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

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

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

是否可以始终在演示文稿控制器中设置视图,并仅在动画过渡对象中设置动画?

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

1 个答案:

答案 0 :(得分:1)

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