【问题标题】: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
    }
}

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

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

【问题讨论】:

    标签: ios swift animation tabbar side-menu


    【解决方案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的应用...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-26
    • 2016-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-28
    相关资源
    最近更新 更多