侧菜单无法在 mapView 上快速正常工作
Posted
技术标签:
【中文标题】侧菜单无法在 mapView 上快速正常工作【英文标题】:Side-menu is not working properly on mapView in swift 【发布时间】:2019-05-23 08:39:37 【问题描述】:我正在快速构建一个应用程序,它可以获取用户的位置并将其保存以供以后访问。我有一个关于用户界面的问题。我在 mapView 上做了一个左侧菜单。当我想滑动打开它时,地图正在滑动,但侧面菜单没有打开。此外,我在顶部栏上有一个 BarButtonItem,如果我从那里滑动,侧面菜单就会打开。我的意思是,我认为问题在于地图的滑动操作以及其他任何事情都无法正常工作。
我从这张图片中的栏按钮项目中滑动它。
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
【问题讨论】:
好的@Zoe,对不起,我不知道。 【参考方案1】:这是一个常见问题 - 地图视图将“消耗”所有触摸事件,这意味着您的手势识别器将永远不会被触发。
解决方案是使用 UIScreenEdgePanGestureRecognizer,完整的解决方案详述如下:
https://***.com/a/24887059
【讨论】:
谢谢@rob,它在打开问题上对我有用。但是关闭侧面菜单时也存在同样的问题。我无法通过拖动关闭菜单。我尝试为此添加 UIPanGestureRecognizer,但我认为这与两个手势识别器存在冲突。我尝试禁用地图的可滚动属性,不幸的是它不起作用。我应该使用任何其他识别器类型吗?以上是关于侧菜单无法在 mapView 上快速正常工作的主要内容,如果未能解决你的问题,请参考以下文章