Swift 如何向侧菜单控制器添加滑动手势?

Swift 如何向侧菜单控制器添加滑动手势?,swift,Swift,我制作了一个带有一些控件的侧菜单,当用户点击侧菜单外部或选择侧菜单内部的一行时,我可以将其关闭。现在,我想在左侧添加一个滑动手势,这样用户也可以通过这种方式消除它 } 我像这样将我的“MenuViewController”实例化到我的主控制器中 如何将SwipegestureRecognitor添加到转换?我感谢你的每一个意见。提前谢谢 我认为不应该在transition对象中实例化viewController。您的ViewController应该具有转换。您可以将手势添加到viewCont

我制作了一个带有一些控件的侧菜单,当用户点击侧菜单外部或选择侧菜单内部的一行时,我可以将其关闭。现在,我想在左侧添加一个滑动手势,这样用户也可以通过这种方式消除它


}


我像这样将我的“MenuViewController”实例化到我的主控制器中


如何将SwipegestureRecognitor添加到转换?我感谢你的每一个意见。提前谢谢

我认为不应该在transition对象中实例化viewController。您的
ViewController
应该具有转换。您可以将手势添加到
viewController
view
中,当值更改时,您将更新转换。我是否必须将手势添加到我的“MainViewController”中,在其中添加侧菜单及其转换,还是添加到“MenuController”中我在哪里声明我要显示的部分?如果
menuController
视图覆盖了整个屏幕,那么您可以将其保留在那里,否则您必须将其添加到您添加为子控制器的位置。我在添加MenuViewController的位置更新了我的答案+
extension MenuViewController {

@objc func dismissControllerAnimated() {
    dismiss(animated: true, completion: nil)
} }

class SlideinTransition: NSObject, UIViewControllerAnimatedTransitioning {

let menuViewController = MenuViewController()

var isPresenting = true
let dimmingView = UIView()

func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
    return 0.5
}

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{

        //adds a tap gesture to our dimming view
        let tapGesture = UITapGestureRecognizer(target: toViewController, action: #selector(MenuViewController.dismissControllerAnimated))
        dimmingView.addGestureRecognizer(tapGesture)

        //adds the dimming view
        dimmingView.backgroundColor = .black
        dimmingView.alpha = 0.0
        containerView.addSubview(dimmingView)
        dimmingView.frame = containerView.bounds
        //adds the menu view controller to our container
        containerView.addSubview(toViewController.view)

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

    let transform = {
        self.dimmingView.alpha = 0.5
        toViewController.view.transform = CGAffineTransform(translationX: finalWidth, y: 0)
    }

    //applies a specific kind of transformation to our view
    let identity = {
        self.dimmingView.alpha = 0.0
        fromViewController.view.transform = .identity
    }

    //animates the transition and cancels it when you click outside of the frame
    let duration = transitionDuration(using: transitionContext)
    let isCancelled = transitionContext.transitionWasCancelled
    UIView.animate(withDuration: duration, animations: {
        self.isPresenting ? transform() : identity()
    }) { (_) in
        transitionContext.completeTransition(!isCancelled)
        if !self.isPresenting {
            self.dimmingView.removeFromSuperview()
        }
    }
}
@IBAction func didTapMenu(_ sender: UIBarButtonItem) {
    guard let menuViewController = storyboard?.instantiateViewController(withIdentifier: "MenuViewController") as? MenuViewController else { return }
    menuViewController.didTapMenuType = { menuType in
        self.transitionToNew(menuType)
    }
    menuViewController.modalPresentationStyle = .overCurrentContext
    menuViewController.transitioningDelegate = self
    present(menuViewController, animated: true)
}