user3766930 user3766930 - 1 year ago 80
iOS Question

how can I make my pins drop on the MKMapView with animation (and possibly - slower than normally)

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"



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?

Answer Source

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

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

Check this

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download