Je travaille sans J'ai une instance de Comment puis-je exécuter une méthode lorsque l'action de chute est effectuée?
Dans cette méthode, j'ai besoin des nouvelles coordonnées de ma vue d'annotation. P> interfaceBuilder code>. p>
mkannotationview code> avec SETDRAGGABLE code> sur oui em>,
Dans mon mkmapview code> Ma vue Annotation s'affiche et je peux le faire glisser et le déposer. P>
3 Réponses :
Si vous avez configuré correctement votre objet Mkannotation avec une méthode SetCoordinate, puis dans la méthode DidChangeDragState, la nouvelle coordonnée doit déjà être dans l'objet d'annotation:
- (void)mapView:(MKMapView *)mapView
annotationView:(MKAnnotationView *)annotationView
didChangeDragState:(MKAnnotationViewDragState)newState
fromOldState:(MKAnnotationViewDragState)oldState
{
if (newState == MKAnnotationViewDragStateEnding)
{
CLLocationCoordinate2D droppedAt = annotationView.annotation.coordinate;
NSLog(@"dropped at %f,%f", droppedAt.latitude, droppedAt.longitude);
}
}
Merci je ne connaissais pas "DidChangeDragstate", et c'était exactement ce que je cherchais.
Vous pouvez également ajouter les éléments suivants:
if (newState == MKAnnotationViewDragStateStarting) {
annotationView.dragState = MKAnnotationViewDragStateDragging;
}
else if (newState == MKAnnotationViewDragStateEnding || newState == MKAnnotationViewDragStateCanceling) {
annotationView.dragState = MKAnnotationViewDragStateNone;
}
Solution SWIFT:
func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, didChange newState: MKAnnotationViewDragState, fromOldState oldState: MKAnnotationViewDragState)
{
if (newState == MKAnnotationViewDragState.ending)
{
let droppedAt = view.annotation?.coordinate
print("dropped at : ", droppedAt?.latitude ?? 0.0, droppedAt?.longitude ?? 0.0);
view.setDragState(.none, animated: true)
}
if (newState == .canceling )
{
view.setDragState(.none, animated: true)
}
}