我有一个代码在 MKMapView 的可见部分显示 10 个自定义引脚:
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
}
生成随机引脚的函数是:
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
使用上面的代码,当用户打开地图时,他看到图钉已经放置在一些随机的地方。我的问题是——我怎样才能让销子一个接一个地掉下来,如果可能的话——我能让它们慢慢地下降,这样每个销子下降 1-2 秒而不是立即运动吗?