2016-11-09 3 views
0

ich einen Code habe, die 10 benutzerdefinierten Pins auf dem sichtbaren Teil des MKMapView zeigt:wie kann ich meine Stifte mit Animation auf dem MKMapView fallen (und möglicherweise - langsamer als normal)

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 

} 

und die Funktion Zufall Stifte zur Erzeugung ist:

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 

mit dem obigen Code, wenn der Benutzer die Karte öffnet, sieht er die Stifte bereits in einigen zufälligen Stellen platziert. Meine Frage ist - wie kann ich die Pins nacheinander fallen lassen, und wenn möglich - kann ich sie langsam fallen lassen, so dass jeder Pin für 1-2 Sekunden fallengelassen wird, anstatt sofort zu bewegen?

Antwort

1

Es ist eine Eigenschaft der Annotation animatesDrop für Animation können Sie einstellen, es ist wahr, wie dieses

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

prüfen this

+0

hmm, wenn ich den Code in meine 'viewfor annotation' Methode eingefügt Ich erhalte Fehler 'Wert des Typs 'MKAnnotationView' hat kein Mitglied 'animatesDrop'' ... Was kann hier falsch sein? – user3766930

+0

Diese Eigenschaft ist für MKPinAnnotationView verfügbar – Rajat

+0

hm ok, in meinem Code verwende ich nicht MKPinAnnotationView ... Ich möchte benutzerdefinierte Bilder für die Pins verwenden, können Sie mir helfen und mir sagen, wie könnte ich meinen Code ändern, um benutzerdefinierte Bilder zu verwenden als Pins und auch die Drop-Animation? – user3766930

Verwandte Themen