2010-04-16 22 views
9

He utilizado imágenes de pin en la aplicación en lugar de pin estándar, ahora quiero dar animación (efecto de caída como lo era con alfileres estándar) a los pines personalizados. ¿Cómo puedo proporcionar un efecto de animación de caída a las imágenes de los pines personalizados ???Animación de pin personalizado - MKMapView

+0

[Esto debería ayudar a] (http://stackoverflow.com/questions/1857160/how-can-i-create-a-custom-pin-drop- animation-using-mkannotationview) – dwery

Respuesta

24

Implemente el siguiente método delegado.

- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views { 
    MKAnnotationView *aV; 
    for (aV in views) { 
    CGRect endFrame = aV.frame; 

    aV.frame = CGRectMake(aV.frame.origin.x, aV.frame.origin.y - 230.0, aV.frame.size.width, aV.frame.size.height); 

    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:0.45]; 
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; 
     [aV setFrame:endFrame]; 
    [UIView commitAnimations]; 

    } 
} 
+5

Esto funcionó muy bien. Debería marcarse la respuesta correcta. –

6

también se siente mucho más fresco si no dejar caer todas las clavijas a la vez, pero deja caer cada una de ellas con un pequeño retraso para que se vea como que hay una lluvia de efecto pasadores. Similar a lo que hace Apple de forma nativa. Utilice:

- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views { 
    MKAnnotationView *aV; 

    float delay = 0.00; 

    for (aV in views) { 
     CGRect endFrame = aV.frame; 

     aV.frame = CGRectMake(aV.frame.origin.x, aV.frame.origin.y - 430.0, aV.frame.size.width, aV.frame.size.height); 
     delay = delay + 0.01; 

     [UIView beginAnimations:nil context:NULL]; 
     [UIView setAnimationDelay:delay]; 
     [UIView setAnimationDuration:0.45]; 
     [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; 
     [aV setFrame:endFrame]; 
     [UIView commitAnimations]; 
    } 
} 
+0

¡Sí, esto es genial! Gracias – PrasadW

0

Esto funcionó bien para mí. No puedo recordar donde lo encontré aquí, aunque

- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views { 
MKAnnotationView *aV; 

for (aV in views) { 

    // Don't pin drop if annotation is user location 
    if ([aV.annotation isKindOfClass:[MKUserLocation class]]) { 
     continue; 
    } 

    // Check if current annotation is inside visible map rect, else go to next one 
    MKMapPoint point = MKMapPointForCoordinate(aV.annotation.coordinate); 
    if (!MKMapRectContainsPoint(self.mapView.visibleMapRect, point)) { 
     continue; 
    } 

    CGRect endFrame = aV.frame; 

    // Move annotation out of view 
    aV.frame = CGRectMake(aV.frame.origin.x, aV.frame.origin.y - self.view.frame.size.height, aV.frame.size.width, aV.frame.size.height); 

    // Animate drop 
    [UIView animateWithDuration:0.3 delay:0.03*[views indexOfObject:aV] options:UIViewAnimationCurveLinear animations:^{ 

     aV.frame = endFrame; 

     // Animate squash 
    }completion:^(BOOL finished){ 
     if (finished) { 
      [UIView animateWithDuration:0.05 animations:^{ 
       aV.transform = CGAffineTransformMakeScale(1.0, 0.8); 

      }completion:^(BOOL finished){ 
       if (finished) { 
        [UIView animateWithDuration:0.5 animations:^{ 
         aV.transform = CGAffineTransformIdentity; 
        }]; 
       } 
      }]; 
     } 
    }]; 
    } 
} 
0

Tenga en cuenta que la animación de la vista de anotación en -mapView:didAddAnnotationViews: causa efectos extraños cuando MKMapView.userTrackingMode es igual a MKUserTrackingModeFollowWithHeading. Solo desearía que Apple hiciera la animación descendente disponible para la clase MKAnnotationView.

0

Swift 3 animación gota

// animate annotation views drop 
func mapView(_ mapView: MKMapView, didAdd views: [MKAnnotationView]) { 
     for annView in views { 

      // animate any annotation views except the user pin 
      if !(annView.annotation?.isKind(of: MKUserLocation.self))! { 
       let endFrame = annView.frame 
       annView.frame = endFrame.offsetBy(dx: 0, dy: -500) 
       UIView.animate(withDuration: 0.5, animations: { 
        annView.frame = endFrame 
       }) 
      } 
     }