swift 的 mapView 上的侧边菜单无法正常工作

Side-menu is not working properly on mapView in swift

我正在 swift 中构建一个应用程序,它获取用户的位置并保存它以供以后访问。我有一个关于 UI 的问题。我在 mapView 上做了一个左侧菜单。当我想滑动打开它时,地图正在滑动但侧面菜单没有打开。此外,我在顶部栏上有一个 BarButtonItem,如果我从那里滑动,则会打开侧边菜单。我的意思是,我认为问题在于地图的滑动操作以及其他任何东西都无法正常工作。

我是从这张图片的bar button item中滑出来的。

override func viewDidLoad() {
    super.viewDidLoad()

    centerViewController = UIStoryboard.userMapViewController()
    centerViewController.delegate = self

    // wrap the centerViewController in a navigation controller, so we can push views to it
    // and display bar button items in the navigation bar
    centerNavigationController = UINavigationController(rootViewController: centerViewController)
    view.addSubview(centerNavigationController.view)
    addChild(centerNavigationController)

    centerNavigationController.didMove(toParent: self)

    let panGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(handlePanGesture(_:)))
    centerNavigationController.view.addGestureRecognizer(panGestureRecognizer)
}

func toggleLeftPanel() {
    let notAlreadyExpanded = (currentState != .leftPanelExpanded)

    if notAlreadyExpanded {
        addLeftPanelViewController()
    }

    animateLeftPanel(shouldExpand: notAlreadyExpanded)
}

func addLeftPanelViewController() {
    guard leftViewController == nil else { return }

    if let vc = UIStoryboard.leftViewController() {
        addChildSidePanelController(vc)
        leftViewController = vc
    }
}

func animateLeftPanel(shouldExpand: Bool) {
    if shouldExpand {
        currentState = .leftPanelExpanded
        animateCenterPanelXPosition(
            targetPosition: centerNavigationController.view.frame.width - centerPanelExpandedOffset)
    } else {
        animateCenterPanelXPosition(targetPosition: 0) { _ in
            self.currentState = .leftPanelCollapsed
            self.leftViewController?.view.removeFromSuperview()
            self.leftViewController = nil
        }
    }
}

extension ContainerViewController: UIGestureRecognizerDelegate {
@objc func handlePanGesture(_ recognizer: UIPanGestureRecognizer) {
    let gestureIsDraggingFromLeftToRight = (recognizer.velocity(in: view).x > 0)

    switch recognizer.state {
    case .began:
        if currentState == .leftPanelCollapsed {
            if gestureIsDraggingFromLeftToRight {
                addLeftPanelViewController()
            }
            showShadowForCenterViewController(true)
        }

    case .changed:
        if let rview = recognizer.view {
            rview.center.x = rview.center.x + recognizer.translation(in: view).x
            recognizer.setTranslation(CGPoint.zero, in: view)
        }

    case .ended:
        if let _ = leftViewController,
            let rview = recognizer.view {
            // animate the side panel open or closed based on whether the view
            // has moved more or less than halfway
            let hasMovedGreaterThanHalfway = rview.center.x > view.bounds.size.width
            animateLeftPanel(shouldExpand: hasMovedGreaterThanHalfway)
        }
    default:
        break
    }
}

这是一个常见问题 - 地图视图将 'consume' 所有触摸事件,这意味着您的手势识别器永远不会被触发。

解决方案是使用 UIScreenEdgePanGestureRecognizer,完整的解决方案详述如下: