gpt4 book ai didi

ios - 是否可以有一个可以拖动或选择的点注释 View ?

转载 作者:行者123 更新时间:2023-11-29 00:33:08 24 4
gpt4 key购买 nike

我有一个 MKMapView,它显示一些 MKPinAnnotationView 对象。
我希望能够拖动注释 View ,但我也希望能够选择它。

问题:

当我实现委托(delegate)函数时

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {…}  

当我着陆注释 View 时,立即调用此函数。它会显示警报,从而防止拖动注释 View 。

当我不实现委托(delegate)功能时,我可以按预期拖动注释 View 。

我认为以下应该是可能的:
- 降落到注释 View 。
- 当我拖动时,移动注释 View 。
- 否则,即当我从注释 View 中触摸时,选择它。

我怎样才能做到这一点?

编辑:

我的图钉注释 View 没有标注。相反,当它被选择时,它会显示一个 UIAlertController 以便用户可以选择进一步的操作。如果是这样,mapView 会变暗并且无法访问。

我想要实现的行为是:

如果我触摸引脚注释 View (手指向下),则不会发生任何事情。

如果我随后移动手指(仍然向下),则应拖动引脚注释 View 。如果我随后抬起手指,则将不会选择图钉注释 View 。

但是,如果我不调整手指,而只是抬起它,则将选择图钉注释 View (并且应显示警报 View )。

我希望这能澄清情况。

最佳答案

一个可能的解决方案是自己处理拖动并使用长按手势来管理您想要的操作(例如何时显示警报)。

此解决方案是围绕 Rob 的详细回答 here 构建的并添加一些额外的逻辑来处理您的问题的细节(即 wasMoved 实例属性)。

private var startLocation = CGPoint(x: 0.0, y: 0.0)
private var wasMoved = false

func handleLongPress(_ sender: UILongPressGestureRecognizer) {
let location = sender.location(in: mapView)

switch sender.state {
case .began:
startLocation = location
case .changed:
wasMoved = true
sender.view?.transform = CGAffineTransform(translationX: location.x - startLocation.x, y: location.y - startLocation.y)
case .ended, .cancelled:
if wasMoved {
let annotationView = sender.view as! MKAnnotationView
let annotation = annotationView.annotation as! MKPointAnnotation

let translate = CGPoint(x: location.x - startLocation.x, y: location.y - startLocation.y)
let originalLocation = mapView.convert(annotation.coordinate, toPointTo: mapView)
let updatedLocation = CGPoint(x: originalLocation.x + translate.x, y: originalLocation.y + translate.y)

annotationView.transform = CGAffineTransform.identity
annotation.coordinate = mapView.convert(updatedLocation, toCoordinateFrom: mapView)
} else {
let alert = UIAlertController(title: "Alert", message: "Here is my alert!", preferredStyle: .alert)

let ok = UIAlertAction(title: "OK", style: .default, handler: nil)
alert.addAction(ok)

present(alert, animated: true, completion: nil)
}
wasMoved = false
default:
break
}
}

你的 mapView(_:viewFor:) 委托(delegate)方法看起来像:

extension ViewController: MKMapViewDelegate {
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
guard let reusablePin = mapView.dequeueReusableAnnotationView(withIdentifier: "Pin") as? MKPinAnnotationView else {
let pin = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "Pin")

// Add the long press gesture recognizer to the annotation view
let longPress = UILongPressGestureRecognizer(target: self, action: #selector(handleLongPress(_:)))
longPress.minimumPressDuration = 0
pin.addGestureRecognizer(longPress)

return pin
}

reusablePin.annotation = annotation
return reusablePin
}
}

关于ios - 是否可以有一个可以拖动或选择的点注释 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41252239/

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