MKMapView, animateDrop?

后端 未结 3 1718
盖世英雄少女心
盖世英雄少女心 2020-12-09 04:37

I have setup an NSMutableArray of objects derived from a class that conforms to the MKAnnotation protocol. I have setup setup title and subtitle for the annotation and have

相关标签:
3条回答
  • 2020-12-09 05:29

    MKPinAnnotationView is a subclass of MKAnnotationView.

    MKAnnotationView is a generic annotation view for which you have to provide the image and animation if desired.

    MKPinAnnotationView is a convenient subclass of MKAnnotationView which automatically provides a pin image in a selected color and an animation of the pin dropping onto the map. You set the animatesDrop property when creating the view in viewForAnnotation and it will handle the animation automatically from there.

    If you don't implement viewForAnnotation, a standard red pin with no animation is displayed.

    By the time didAddAnnotationViews is called, the automatic animation has already happened and setting that property there has no effect.

    If however you want to create a custom animation different from the default drop animation that MKPinAnnotationView provides, you could do that in didAddAnnotationViews. The view will already be at its final destination point so you save that and then animate it from a different point to that destination.

    If you're happy with the default drop animation that MKPinAnnotationView provides, you don't need to implement didAddAnnotationViews. That delegate method is more useful for doing other things that you might need to do when all the annotation views are actually in place.

    For your pins to show the title, set canShowCallout to YES where you set animatesDrop.

    Not sure what you mean by "this creates a new set of MKAnnotationViews". In the viewForAnnotation method, you are providing a view (MKPinAnnotationView) for the MKAnnotation object. They are not the same thing.

    Also, the viewForAnnotation method works like the cellForRowAtIndexPath method for UITableView where annotation views can get recycled which is why it's important to set MKAnnotation-specific information in the view every time (such as the annotation property).

    0 讨论(0)
  • 2020-12-09 05:31

    Update Swift3:

     func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    
      if annotation is MKUserLocation {
         return nil
      }
    
      let pinAnnotation = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "PinAnnotation")
      pinAnnotation.pinTintColor = .green;
      pinAnnotation.animatesDrop = true;
      pinAnnotation.canShowCallout = false;
      pinAnnotation.setSelected(true, animated: true)
    
      return pinAnnotation
    }
    
    0 讨论(0)
  • 2020-12-09 05:36

    Here's the most simple solution I could find. What it does is drops a single pin on the UIMapView in viewDidLoad event.

    1. The project references MapKit framework

    2. The view have the following import:#import <MapKit/MapKit.h>

    3. The view controller implements MKMapViewDelegate protocol

    4. The view controller implementation contains:

    - (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id ) annotation
    {
        MKPinAnnotationView *newAnnotation = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"annotation1"];
        newAnnotation.pinColor = MKPinAnnotationColorGreen;
        newAnnotation.animatesDrop = YES; 
        newAnnotation.canShowCallout = NO;
        [newAnnotation setSelected:YES animated:YES]; 
        return newAnnotation;
    }
    
    1. The viewDidLoad contains:
    CLLocationCoordinate2D geos = CLLocationCoordinate2DMake(0.2344, 45.324);
    MKPlacemark* marker = [[MKPlacemark alloc] initWithCoordinate:geos addressDictionary:nil];
    [mapView addAnnotation:marker];
    [marker release];
    
    0 讨论(0)
提交回复
热议问题