Swift:左侧滑动的判断,连动。

HomeViewController.swift


func setBarUI() {

        ...

        //一定要将menuView.view添加进去

        UIApplication.shared.keyWindow?.addSubview(menuView.view)

        menuView.bindtoNav = navigationController?.tabBarController

        view.addGestureRecognizer(UIPanGestureRecognizer(target:self, action:#selector(panGesture(pan:))))

        menuView.view.addGestureRecognizer(UIPanGestureRecognizer(target:self,     action:#selector(panGesture(pan:))))

    }


  func panGesture(pan: UIPanGestureRecognizer) {

        menuView.panGesture(pan: pan)

    }



MenuViewController.swift


var bindtoNav: UITabBarController?


let menuView = MenuViewController.shareInstance

  var showView = false {

        didSet {

            showView ? showMenu() : dismissMenu()

        }

    }



extension MenuViewController {

    

    static let shareInstance = createMenuView()

    

    private static func createMenuView() -> MenuViewController {

        let storyboard = UIStoryboard.init(name: "Main", bundle: Bundle.main)

        let menuView = storyboard.instantiateViewController(withIdentifier: "MenuViewController") as? MenuViewController

        menuView?.view.frame = CGRect.init(x: -225, y: 0, width: 225, height: screenH)

        return menuView!

    

 func panGesture(pan: UIPanGestureRecognizer) {

        let xoff = pan.translation(in: view).x

        if pan.state == .began {

            beganDate = Date()

        }

        if pan.state == .ended {

            endDate = Date()

            //区分是轻扫还是滑动,nanoseconds: 毫微秒 十亿分之一秒

            if endDate! < beganDate! + 150000000.nanoseconds {

                if xoff > 0 {

                    showView = true

                } else {

                    showView = false

                }

                return

            }

        }

        if (0 < xoff && xoff <= 225 && !showView) || (0 > xoff && xoff >= -225 && showView) {

            if pan.translation(in: view).x > 0 {

                moveMenu(pan.translation(in: view).x)

            } else {

                moveMenu(225 + pan.translation(in: view).x)

            }

            if pan.state == .ended {

                if showView {

                    if pan.translation(in: view).x < -175 {

                        showView = false

                    } else {

                        showView = true

                    }

                } else {

                    if pan.translation(in: view).x > 50 {

                        showView = true

                    } else {

                        showView = false

                    }

                }

            }

        }

    }

    

    func moveMenu(_ xoff: CGFloat) {

        let view = UIApplication.shared.keyWindow?.subviews.first

        let menuView = UIApplication.shared.keyWindow?.subviews.last

        UIApplication.shared.keyWindow?.bringSubview(toFront: (UIApplication.shared.keyWindow?.subviews[1])!)

        view?.transform = CGAffineTransform.init(translationX: xoff, y: 0)

        //menuViewview一样的行为

        menuView?.transform = (view?.transform)!

    }

    

    func showMenu() {

        let view = UIApplication.shared.keyWindow?.subviews.first

        let menuView = UIApplication.shared.keyWindow?.subviews.last

        UIApplication.shared.keyWindow?.bringSubview(toFront: (UIApplication.shared.keyWindow?.subviews[1])!)

        UIView.animate(withDuration: 0.5, animations: { 

            view?.transform = CGAffineTransform.init(translationX: 225, y: 0)

            menuView?.transform = (view?.transform)!

        })

    }

    

    func dismissMenu() {

        let view = UIApplication.shared.keyWindow?.subviews.first

        let menuView = UIApplication.shared.keyWindow?.subviews.last

        UIApplication.shared.keyWindow?.bringSubview(toFront: (UIApplication.shared.keyWindow?.subviews[1])!)

        UIView.animate(withDuration: 0.5, animations: {

            view?.transform = CGAffineTransform.init(translationX: 0, y: 0)

            menuView?.transform = (view?.transform)!

        })

    }

    



}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值