我有一个代码,在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秒,而不是立即移动? 有一个用于动画的注释animatesDrop
属性你可以像这样设置它为真
annotationView!.canShowCallout = true
annotationView!.animatesDrop = true
检查这个