如何使用5个以上的ViewControllers为UITabBarController中的转换设置动画?

时间:2017-11-15 07:24:49

标签: ios swift uitabbarcontroller

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

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

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

1 个答案:

答案 0 :(得分:0)

您需要指定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 ...

}

}