如何从标签栏打开侧边菜单

Posted

技术标签:

【中文标题】如何从标签栏打开侧边菜单【英文标题】:How to open side menu from tabbar 【发布时间】:2020-03-21 11:43:16 【问题描述】:

我正在尝试实现一些事情,例如从 tabbar 项目点击打开的侧边菜单。

我使用了下面的过渡动画类...

class SlideInTransition: NSObject, UIViewControllerAnimatedTransitioning 

var isPresenting = false
let dimmingView = UIView()

func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval 
    return 3


func animateTransition(using transitionContext: UIViewControllerContextTransitioning) 

    guard let toViewController = transitionContext.viewController(forKey: .to),
        let fromViewController = transitionContext.viewController(forKey: .from) else  return 

    let containerView = transitionContext.containerView

    let finalWidth = toViewController.view.bounds.width * 0.3
    let finalHeight = toViewController.view.bounds.height

    if isPresenting 
        // Add dimming view
        dimmingView.backgroundColor = .black
        dimmingView.alpha = 0.0
        containerView.addSubview(dimmingView)
        dimmingView.frame = containerView.bounds
        // Add menu view controller to container
        containerView.addSubview(toViewController.view)

        // Init frame off the screen
        toViewController.view.frame = CGRect(x: -finalWidth, y: 0, width: finalWidth, height: finalHeight)
    

    // Move on screen
    let transform = 
        self.dimmingView.alpha = 0.5
        toViewController.view.transform = CGAffineTransform(translationX: finalWidth, y: 0)
    


    // Move back off screen
    let identity = 
        self.dimmingView.alpha = 0.0
        fromViewController.view.transform = .identity
    

    // Animation of the transition
    let duration = transitionDuration(using: transitionContext)
    let isCancelled = transitionContext.transitionWasCancelled
    UIView.animate(withDuration: duration, animations: 
        self.isPresenting ? transform() : identity()
    )  (_) in
        transitionContext.completeTransition(!isCancelled)
    
    



并在我的代码中使用它,如下所示

  guard let menuViewController = storyboard?.instantiateViewController(withIdentifier: "MenuVC") as? MenuVC else  return 
        menuViewController.modalPresentationStyle = .overCurrentContext
        menuViewController.transitioningDelegate = self as? UIViewControllerTransitioningDelegate
        menuViewController.tabBarItem.image = UIImage(named: "ico_menu")
        menuViewController.tabBarItem.selectedImage = UIImage(named: "ico_menu")

        viewControllers = [orderVC,serverdVC,canceledVC,menuViewController]


extension TabbarVC: UIViewControllerTransitioningDelegate 
    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? 
        transiton.isPresenting = true
        return transiton
    

    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? 
        transiton.isPresenting = false
        return transiton
    

但动画根本不起作用......我想在当前上下文中像侧面菜单一样打开它......

我怎样才能做到这样的事情......

【问题讨论】:

【参考方案1】:

TabBar 不能仅处理单个子视图控制器的动画过渡。如果您应用自定义过渡,它将应用于其所有选项卡(子视图控制器)。加上我上次检查时,打开用户个人资料时,airbnb 的应用程序的行为并非如此。 :)

不过,您可以做的是在导航视图控制器的顶部或任何地方有一个单独的菜单按钮,然后从那里调用幻灯片:

    func slideInView() 
        let vcToShow = MenuViewController()
        vcToShow.modalPresentationStyle = .overCurrentContext
        vcToShow.transitioningDelegate = self as? UIViewControllerTransitioningDelegate
        present(vcToShow, animated: true, completion: nil)
    

或者,如果您坚持将菜单作为选项卡的一部分,那么您可以使用this。

希望这会有所帮助。 :)

【讨论】:

是的,我可以通过func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) 方法打开带有动画的菜单...但它也可以在后台打开,因为tabbar 我无法通过放置一个没有@ 的额外按钮来处理这个问题987654325@ ...也许我必须自己做个习惯tabbar ...是的,这个屏幕截图来自谷歌而不是来自airbnb的应用...

以上是关于如何从标签栏打开侧边菜单的主要内容,如果未能解决你的问题,请参考以下文章

如何在另一个侧边菜单项中显示标签栏?

在导航到另一个视图控制器之前切换标签栏

vue+elementui搭建后台管理界面(3侧边栏菜单)

UltraEdit - 怎么显示文件标签栏和侧边栏

若依 vue前端 动态设置路由path不同参数 在页面容器里打开新页面(新路由),面包屑和标签页标题根据参数动态改变,面包屑多级标题,侧边栏对应菜单亮起

从ajax侧边栏加载的带有bootstrap-4动态侧边栏菜单的Adminlte 3打开不起作用