滑动菜单打开滑动和按钮

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了滑动菜单打开滑动和按钮相关的知识,希望对你有一定的参考价值。

我有一个幻灯片菜单。按下按钮打开菜单。我也需要添加滑动来打开菜单。我做两个功能。但打开了两个菜单 - 一个按下按钮,另一个按下时)))我需要打开一个菜单。请帮帮我

它的代码和SWIPE的图像代码

 [@objc public func onSlideMenuButtonPressed(_ sender : UIButton){
        if (sender.tag == 10)
        {
            // To Hide Menu If it already there

            self.slideMenuItemSelectedAtIndex(-1);

            sender.tag = 0;

            let viewMenuBack : UIView = view.subviews.last!

            UIView.animate(withDuration: 0.3, animations: { () -> Void in
                var frameMenu : CGRect = viewMenuBack.frame
                frameMenu.origin.x = -1 * UIScreen.main.bounds.size.width
                viewMenuBack.frame = frameMenu
                viewMenuBack.layoutIfNeeded()
                viewMenuBack.backgroundColor = UIColor.clear
                }, completion: { (finished) -> Void in
                    viewMenuBack.removeFromSuperview()
            })

            return
        }

        sender.isEnabled = false
        sender.tag = 10

        let menuVC : MenuViewController = self.storyboard!.instantiateViewController(withIdentifier: "MenuViewController") as! MenuViewController
        menuVC.btnMenu = sender
        menuVC.delegate = self
        self.view.addSubview(menuVC.view)
        self.addChildViewController(menuVC)
        menuVC.view.layoutIfNeeded()


        menuVC.view.frame=CGRect(x: 0 - UIScreen.main.bounds.size.width, y: 0, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height);

        UIView.animate(withDuration: 0.3, animations: { () -> Void in
            menuVC.view.frame=CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height);
            sender.isEnabled = true
            }, completion:nil)
    }


    public func openMenu(){

        SwipeRigtYES = true
//        sender.isEnabled = false
//        sender.tag = 10
        let menuVC : MenuViewController = self.storyboard!.instantiateViewController(withIdentifier: "MenuViewController") as! MenuViewController
//        menuVC.btnMenu = sender
        menuVC.delegate = self
        self.view.addSubview(menuVC.view)
        self.addChildViewController(menuVC)

    }

    public func closeMenu(){
        SwipeRigtYES = false

        let viewMenuBack : UIView = view.subviews.last!

        UIView.animate(withDuration: 0.3, animations: { () -> Void in
            var frameMenu : CGRect = viewMenuBack.frame
            frameMenu.origin.x = -1 * UIScreen.main.bounds.size.width
            viewMenuBack.frame = frameMenu
            viewMenuBack.layoutIfNeeded()
            viewMenuBack.backgroundColor = UIColor.clear
        }, completion: { (finished) -> Void in
            viewMenuBack.removeFromSuperview()
        })

然后滑动

 @objc func handleGesture(gesture: UISwipeGestureRecognizer) -> Void {
        if gesture.direction == UISwipeGestureRecognizerDirection.right {
            print("Swipe Right")
            if SwipeRigtYES==false {
                 openMenu()
                print("open")
            }else {
            print("Else")
        }




        }
        else if gesture.direction == UISwipeGestureRecognizerDirection.left  {
            print("Swipe Left")
            if SwipeRigtYES==true {
                closeMenu()


                print("close")
            } else {
                print("Else")
            }

请帮助我也许我需要一个带有点击按钮的功能和滑动操作来打开菜单

答案

首先将menuView作为主VC中的var,然后在执行菜单动画时制作bool,省略任何后续点击,直到动画结束动作发送者是滑动还是单击按钮

以上是关于滑动菜单打开滑动和按钮的主要内容,如果未能解决你的问题,请参考以下文章

如何使用导航抽屉打开和关闭更改按钮图像

Xcode AMSlideMenu 仅使用按钮打开左侧菜单,禁用滑动

我实施滑动标签有什么问题?

我的应用程序使用片段,但我需要从左到右从右向左滑动

侧边菜单滑动功能

我的片段中有 webview,当滑动 webview 时, viewPager 也开始滑动