当前带有自定义动画的嵌入在导航控制器中的视图控制器

时间:2018-12-12 08:47:35

标签: ios swift uinavigationcontroller uikit uimodalpresentationstyle

我有vc1,它是当前的导航控制器,其中包含vc2。 vc1和导航控制器之间的设置是“以模态形式呈现”,vc2从底部到顶部以标准动画显示。

我想用自定义过渡动画从vc1中展示嵌入到导航控制器中的vc2。

Storyboard sample

我尝试过

class CustomAnimator: NSObject {
    func animationDuration() -> TimeInterval {
        return 1.0
    }

    fileprivate func animateCustomTransition(using transitionContext: UIViewControllerContextTransitioning) {
        // ...
    }
}

extension CustomAnimator: UIViewControllerAnimatedTransitioning {
    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        return animationDuration()
    }

    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        animateCustomTransition(using: transitionContext)
    }
}

class CustomTransitionDelegate: NSObject {
    let animator: CustomAnimator
    override init() {
        animator = CustomAnimator()
        super.init()
    }
}

extension CustomTransitionDelegate: UIViewControllerTransitioningDelegate {
    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return self.animator
    }
}

class Vc2ViewController: UIViewController {

    // ...

    var customTranstionDelegate = CustomTransitionDelegate()

    //...
}

然后:

(1)设置vc2的transitioningDelegate。显然没有效果。

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let navController = segue.destination as? UINavigationController {
        if let controller = navController.viewControllers.first as? Vc2ViewController {
            controller.transitioningDelegate = controller.customTranstionDelegate
        }
    }
}

(2)子类化UINavigationController并将其设置为transitioningDelegate。 vc2以所需的方式显示,但是导航栏消失了,并且vc2之后的下一个视图控制器没有出现在“显示”按钮上。

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let controller = segue.destination as? Vc2NavigationController {
        controller.transitioningDelegate = controller.customTranstionDelegate
    }
}

如何从具有自定义过渡动画的vc1中呈现嵌入在导航控制器中的vc2?

1 个答案:

答案 0 :(得分:0)

您可以使用嵌入式-https://developer.apple.com/documentation/uikit/uiviewcontrolleranimatedtransitioning

  

要将用户交互添加到视图控制器过渡中,必须将动画器对象与交互式动画器对象(使用UIViewControllerInteractiveTransitioning协议的自定义对象)一起使用。