如何正确地为 MKPointAnnotation 的坐标变化设置动画
How to animate an coordinate change of an MKPointAnnotation correctly
我想像这样在地图视图上为 MKPointAnnotation 的坐标变化设置动画:
let destinationLocation = CLLocationCoordinate2D(latitude: 51.873983, longitude: 12.627966)
UIView.animate(withDuration: 20, animations: {
self.myAnnotation.coordinate = destinationLocation
}, completion: nil)
我在地图上添加了这样的图钉:
let startPosition = CLLocationCoordinate2D(latitude: 53.014542, longitude: 13.996593)
myAnnotation.coordinate = startPosition
mapView.addAnnotation(myAnnotation)
myAnnotation 是在我的 ViewController:
中这样声明的全局变量
private let myAnnotation = MKPointAnnotation()
但是当我在动画期间缩放地图时(调用 MKMapViewDelegate 的方法 regionDidChangeAnimated),图钉的位置移动到地图上的错误位置,而不是在缩放期间移动到正确的地图坐标。
我在这里上传了一个截屏视频,它很好地描述了这个问题。
我不知道提供短视频的另一种可能性。大小只有 8 MB。这是查看问题的最佳方式。
每当用户与地图交互(缩放、滚动)时,您 could/should 停止动画
if let annotationView = self.mapView.view(for: self.myAnnotation) {
annotationView.layer.removeAllAnimations()
}
此外,您可以实现动画的完成处理程序以正确地作用于 canceled/successful 动画:
UIView.animate(withDuration: 20, animations: {
self.myAnnotation.coordinate = destinationLocation
}, completion: { success in
if success {
// handle a successfully ended animation
} else {
// handle a canceled animation, i.e move to destination immediately
self.myAnnotation.coordinate = destinationLocation
}
})
我想像这样在地图视图上为 MKPointAnnotation 的坐标变化设置动画:
let destinationLocation = CLLocationCoordinate2D(latitude: 51.873983, longitude: 12.627966)
UIView.animate(withDuration: 20, animations: {
self.myAnnotation.coordinate = destinationLocation
}, completion: nil)
我在地图上添加了这样的图钉:
let startPosition = CLLocationCoordinate2D(latitude: 53.014542, longitude: 13.996593)
myAnnotation.coordinate = startPosition
mapView.addAnnotation(myAnnotation)
myAnnotation 是在我的 ViewController:
中这样声明的全局变量private let myAnnotation = MKPointAnnotation()
但是当我在动画期间缩放地图时(调用 MKMapViewDelegate 的方法 regionDidChangeAnimated),图钉的位置移动到地图上的错误位置,而不是在缩放期间移动到正确的地图坐标。
我在这里上传了一个截屏视频,它很好地描述了这个问题。
我不知道提供短视频的另一种可能性。大小只有 8 MB。这是查看问题的最佳方式。
每当用户与地图交互(缩放、滚动)时,您 could/should 停止动画
if let annotationView = self.mapView.view(for: self.myAnnotation) {
annotationView.layer.removeAllAnimations()
}
此外,您可以实现动画的完成处理程序以正确地作用于 canceled/successful 动画:
UIView.animate(withDuration: 20, animations: {
self.myAnnotation.coordinate = destinationLocation
}, completion: { success in
if success {
// handle a successfully ended animation
} else {
// handle a canceled animation, i.e move to destination immediately
self.myAnnotation.coordinate = destinationLocation
}
})