【问题标题】:Custom view controller presentation without animation没有动画的自定义视图控制器演示
【发布时间】:2017-03-26 16:37:35
【问题描述】:

我有一些自定义模式演示和自定义控制器要演示(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()!)
    }
}

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

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

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

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

【问题讨论】:

    标签: ios uikit uiviewanimationtransition presentviewcontroller uipresentationcontroller


    【解决方案1】:

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

    【讨论】:

    • 您能详细说明一下吗?我正在尝试同样的事情。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-22
    • 1970-01-01
    相关资源
    最近更新 更多