我怎样才能让我的图钉落在带有动画的 MKMapView 上(并且可能 - 比平时慢)
how can I make my pins drop on the MKMapView with animation (and possibly - slower than normally)
我有一个代码可以在 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
勾选this
我有一个代码可以在 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
勾选this