【问题标题】:How to animate transitions in a UITabBarController using more than 5 ViewControllers?如何使用超过 5 个 ViewController 为 UITabBarController 中的过渡设置动画?
【发布时间】:2018-04-28 08:03:54
【问题描述】:

我有一个UITabBarController,它有 9 个视图控制器,我自定义处理带有前进和后退按钮的导航。我创建了一个UIViewControllerAnimatedTransitioning 对象来处理动画(从左到右简单),它在委托方法中返回:

tabBarController(_ tabBarController: UITabBarController, animationControllerForTransitionFrom fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning?

但是,在选择第五个索引之后,不再调用此函数(因为它现在由 UIMoreNavigationController 处理。是否有动画委托或我应该为 UIMoreNavigationController 实例处理它的某种方式?

【问题讨论】:

  • 你有没有考虑过使用 UIPageViewController,而不是 UITabBarController?
  • 一开始我应该将它作为一个 UIPageViewController 来完成,我可能不得不将我的代码迁移到它,但这样做会付出相当大的努力。所以我希望有一个更简单的方法。

标签: ios swift uitabbarcontroller


【解决方案1】:

您需要指定 moreNavigationController 的委托。因此,在您的 UITabBarController 类中,您需要:

        self.moreNavigationController.delegate = strongDelegate // where strongDelegate is a local instance of MyControllerDelegate as defined below

然后委托应该实现 navigationController 函数,该函数将调用适用的 UIViewControllerAnimatedTransitioning 实例:

class MyControllerDelegate: NSObject, UINavigationControllerDelegate {

func navigationController(_ navigationController: UINavigationController, animationControllerFor operation: UINavigationController.Operation, from fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
    switch operation {
    case .push:
        return MyPresentationAnimationController(isPresenting: true)
    case .pop:
        return MyPresentationAnimationController(isPresenting: false)
    default:
        return nil
    }
}

}

您的 animateTransition 函数包含发生魔法的逻辑:

class MyPresentationAnimationController : NSObject, UIViewControllerAnimatedTransitioning {

private let isPresenting: Bool

init(isPresenting: Bool) {
    self.isPresenting = isPresenting
}

func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
    return TimeInterval(UINavigationController.hideShowBarDuration)
}

func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
    guard
        let toView = transitionContext.view(forKey: .to),
        let fromView = transitionContext.view(forKey: .from)
        else {
            return
    }
    let containerView = transitionContext.containerView

    // TODO: Implement the logic with UIView.animateKeyframes here ...

}

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-12
    • 1970-01-01
    • 2017-12-06
    • 1970-01-01
    • 2022-01-13
    • 2021-05-23
    • 1970-01-01
    • 2016-04-11
    相关资源
    最近更新 更多