0

I have a code that displays 10 custom pins on the visible part of the MKMapView:

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {

    if !(annotation is MKPointAnnotation) {
        return nil
    }

    var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "demo")
    if annotationView == nil {
        annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "demo")
        annotationView!.canShowCallout = true
    }
    else {
        annotationView!.annotation = annotation
    }

    annotationView!.image = UIImage(named: "OtherPin")
    return annotationView

} 

and the function for generating random pins is:

func addPinsToMap(mapView: MKMapView, amount howMany:Int) {

//First we need to calculate the corners of the map so we get the points
    let nePoint:CGPoint = CGPoint(mapView.bounds.origin.x + mapView.bounds.size.width, mapView.bounds.origin.y);
    let swPoint:CGPoint = CGPoint((mapView.bounds.origin.x), (mapView.bounds.origin.y + mapView.bounds.size.height));

//Then transform those point into lat,lng values
    let neCoord:CLLocationCoordinate2D = mapView.convert(nePoint, toCoordinateFrom: mapView)
    let swCoord:CLLocationCoordinate2D = mapView.convert(swPoint, toCoordinateFrom: mapView)

// Loop
for _ in 0 ..< howMany {

    let latRange:Double = Double(self.randomBetweenNumbers(firstNum: CGFloat(neCoord.latitude), secondNum: CGFloat(swCoord.latitude)))
    let longRange:Double = Double(self.randomBetweenNumbers(firstNum: CGFloat(neCoord.longitude), secondNum: CGFloat(swCoord.longitude)))

// Add new waypoint to map
    let location:CLLocationCoordinate2D = CLLocationCoordinate2DMake(latRange, longRange);

    let annotation = MKPointAnnotation()
    //let centerCoordinate = CLLocationCoordinate2D(latitude: 41, longitude:29)
    annotation.coordinate = location
    annotation.title = "Title"
    mapView.addAnnotation(annotation)

}//end

}//end

with the code above when user opens the map, he sees the pins already placed in some random places. My question is - how can I make the pins drop one after another, and if possible - can I make them drop slowly, so that each pin drops for 1-2 seconds instead of immediate motion?

user3766930
  • 5,629
  • 10
  • 51
  • 104

1 Answers1

2

There is a property of annotation animatesDrop for animation you can set it true like this

annotationView!.canShowCallout = true
annotationView!.animatesDrop = true

Check this

Rajat
  • 10,977
  • 3
  • 38
  • 55
  • hmm when I pasted your code into my `viewfor annotation` method I'm getting error `Value of type 'MKAnnotationView' has no member 'animatesDrop'`... What can be wrong here? – user3766930 Nov 09 '16 at 01:44
  • That property is available for MKPinAnnotationView – Rajat Nov 09 '16 at 01:50
  • hm ok, in my code I'm not using MKPinAnnotationView... I want to use custom images for the pins, could you help me and tell me how could I change my code to use custom images as pins and also the drop animation? – user3766930 Nov 09 '16 at 01:51
  • Check this - http://stackoverflow.com/questions/6808876/how-do-i-animate-mkannotationview-drop – Rajat Nov 09 '16 at 01:56
  • Thanks Rajat, I implemented method `didAddAnnotationViews` like they suggested, but now when I run the app I'm never entering this method... how can I reach it? – user3766930 Nov 09 '16 at 02:08
  • Which version of swift you are using ? – Rajat Nov 09 '16 at 02:14
  • I'm using swift 3.0 – user3766930 Nov 09 '16 at 02:14
  • hm but the method starts from `for view in views {` and when I use your declaration (`optional func mapView(_ mapView: MKMapView, didUpdate userLocation: MKUserLocation)`) - I don't have access to views... – user3766930 Nov 09 '16 at 02:24
  • 1
    Sorry I just pasted the wrong method. The right method is - `optional func mapView(_ mapView: MKMapView, didAdd views: [MKAnnotationView]) {}` – Rajat Nov 09 '16 at 02:28