가능한 해결 방법 중 하나는 직접 드래그를 처리하고 길게 누르는 제스처를 사용하여 원하는 작업 (예 : 경고 표시시기)을 관리하는 것입니다.
이 솔루션은 롭의 자세한 답변을 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:)
위임 방법과 같을 것이다 : "경고 표시"로,
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
}
}
해명의 몇 가지, 당신은 주석보기가 선택 될 때 선이 표시되고 있음을 의미합니까? 또한, "터치 업"이라고 할 때 손가락을 들어 올릴 때 (터치 다운하려면 반대)를 의미합니까? –
주석을 선택하고 두 개의 다른 동작으로 드래그하는 것을 볼 수 있습니다. 표준 핀 주석보기를 사용하여 설명 선을 표시 할 핀을 선택한 다음 핀을 다시 선택하여 드래그를 시작합니다. 사용자가 핀을 드래그하지 않으려면 설명 선을보기 위해 핀을 선택할 수 있습니다. –
죄송합니다. 내 질문에 명확하지 않았습니다. 나는 그것을 새롭게 할 것이다. –