gpt4 book ai didi

ios - 使用自定义动画呈现嵌入导航 Controller 中的 View Controller

转载 作者:行者123 更新时间:2023-11-30 10:57:52 25 4
gpt4 key购买 nike

我有 vc1,它正在呈现导航 Controller ,其中包含 vc2。 vc1和导航 Controller 之间的segue是“模态呈现”,vc2以标准动画从下到上出现。

我想通过自定义过渡动画从 vc1 呈现嵌入导航 Controller 的 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 之后的下一个 View Controller 未出现在“显示”segue 上。

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

如何使用自定义过渡动画从 vc1 呈现嵌入导航 Controller 的 vc2?

最佳答案

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

To add user interaction to a view controller transition, you must use an animator object together with an interactive animator object—a custom object that adopts the UIViewControllerInteractiveTransitioning protocol.

关于ios - 使用自定义动画呈现嵌入导航 Controller 中的 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53739144/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com