【发布时间】:2018-03-28 14:41:59
【问题描述】:
使用 Swift 4 在 iOS 中的 MKAnnotationView 动画
为引脚添加动画以添加引脚以平滑映射。 为地图添加图钉时,不流畅或没有动画效果。 有没有简单的解决方案来实现这一点?
【问题讨论】:
标签: ios mkmapview uiviewanimation mkannotationview mkpointannotation
使用 Swift 4 在 iOS 中的 MKAnnotationView 动画
为引脚添加动画以添加引脚以平滑映射。 为地图添加图钉时,不流畅或没有动画效果。 有没有简单的解决方案来实现这一点?
【问题讨论】:
标签: ios mkmapview uiviewanimation mkannotationview mkpointannotation
我用简单的代码试了一下
我在 swift 4 中创建了一个简单的代码来解决这个问题。
这是我的代码,
像这样覆盖你的 viewForAnnotaion 方法
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
var annotationView = self.mapView.dequeueReusableAnnotationView(withIdentifier: "Pin")
if annotationView == nil{
annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "Pin")
}else{
annotationView?.annotation = annotation
}
annotationView?.canShowCallout = false
guard !annotation.isKind(of: MKUserLocation.self) else {
//for the custom image on current location
annotationView?.image = #imageLiteral(resourceName: "currentLocationPin")
return annotationView
}
annotationView.image = UIImage(named: "pinGreen")
let scaleTransform = CGAffineTransform(scaleX: 0.0, y: 0.0) // Scale
UIView.animate(withDuration: 0.2, animations: {
annotationView?.transform = scaleTransform
annotationView?.layoutIfNeeded()
}) { (isCompleted) in
// Nested block of animation
UIView.animate(withDuration: 0.3, animations: {
annotationView?.alpha = 1.0
annotationView?.transform = CGAffineTransform(scaleX: 1.0, y: 1.0)
AnimationUtility.viewSlideInFromBottom(toTop: annotationView!)
annotationView?.layoutIfNeeded()
})
}
return annotationView
}
AnimationUtility 类参考此链接
https://stackoverflow.com/a/49398148/8334818
此代码将引脚添加到具有平滑动画的地图。
【讨论】: