E 'possibile chiamare animatesDrop in un MKAnnotationView piuttosto che MKPinAnnotationView?
-
22-09-2019 - |
Domanda
Lo sai che MKPinAnnotationView ha un metodo "animatesDrop" per animare un'annotazione passaggio di cima al punto sulla mappa con un'ombra? OK, è possibile fare questo un'immagine personalizzata ??
conSoluzione
Si può sempre fare l'animazione personalizzata in modo MKMapViewDelegate.
- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views
Probabilmente qualcosa di simile (non sarà possibile ottenere l'animazione ombra di fantasia, se si vuole che dovete farlo da soli):
- (void) mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views {
CGRect visibleRect = [mapView annotationVisibleRect];
for (MKAnnotationView *view in views) {
CGRect endFrame = view.frame;
CGRect startFrame = endFrame; startFrame.origin.y = visibleRect.origin.y - startFrame.size.height;
view.frame = startFrame;
[UIView beginAnimations:@"drop" context:NULL];
[UIView setAnimationDuration:1];
view.frame = endFrame;
[UIView commitAnimations];
}
}
Swift Version
func mapView(mapView: MKMapView, didAddAnnotationViews views: [MKAnnotationView]) {
let visibleRect = mapView.annotationVisibleRect
for view:MKAnnotationView in views{
let endFrame:CGRect = view.frame
var startFrame:CGRect = endFrame
startFrame.origin.y = visibleRect.origin.y - startFrame.size.height
view.frame = startFrame;
UIView.beginAnimations("drop", context: nil)
UIView.setAnimationDuration(1)
view.frame = endFrame;
UIView.commitAnimations()
}
}
Altri suggerimenti
@gcamp solo in Swift per coloro che sono interessati
func mapView(mapView: MKMapView, didAddAnnotationViews views: [MKAnnotationView]) {
let visibleRect = mapView.annotationVisibleRect
for view:MKAnnotationView in views{
let endFrame:CGRect = view.frame
var startFrame:CGRect = endFrame
startFrame.origin.y = visibleRect.origin.y - startFrame.size.height
view.frame = startFrame;
UIView.beginAnimations("drop", context: nil)
UIView.setAnimationDuration(1)
view.frame = endFrame;
UIView.commitAnimations()
}
}
Grazie @gcamp per la risposta, funziona benissimo, ma ho modificare un po 'per essere precisi per cui la vista verrà abbandonato su MapView, controllare il codice qui sotto:
- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views
{
CGRect visibleRect = [mapView annotationVisibleRect];
for (MKAnnotationView *view in views)
{
CGRect endFrame = view.frame;
endFrame.origin.y -= 15.0f;
endFrame.origin.x += 8.0f;
CGRect startFrame = endFrame;
startFrame.origin.y = visibleRect.origin.y - startFrame.size.height;
view.frame = startFrame;
[UIView beginAnimations:@"drop" context:NULL];
[UIView setAnimationDuration:0.2];
view.frame = endFrame;
[UIView commitAnimations];
}
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow