我正在快速构建一个应用程序,该应用程序可以获取用户的位置并将其保存以供以后访问。我对UI有问题。我在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
}
}
答案 0 :(得分:0)
这是一个常见问题-地图视图将“消耗”所有触摸事件,这意味着您的手势识别器将永远不会被触发。
解决方案是使用UIScreenEdgePanGestureRecognizer,完整的解决方案如下所示: