gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 10:43:42 25 4
gpt4 key购买 nike

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

我从这张图片中的栏按钮项目中滑动它。

enter image description here

enter image description here

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
}
}

最佳答案

这是一个常见问题 - map View 将“消耗”所有触摸事件,这意味着您的手势识别器永远不会被触发。

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

https://stackoverflow.com/a/24887059

关于swift - 侧菜单无法在 swift 的 mapView 上正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56271265/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com