我有两个自定义MKAnnotationViews
,当它们最初添加到MKMapView
时显示正常。
问题是,当旋转或缩小时,他们绘制的位置似乎变得越来越偏离。重申一下,我越接近放大,它们就越准确,但是当缩小和旋转时它们完全关闭。有什么想法吗?
看起来不错(初始状态):
看起来很糟糕(和错误):
我的viewForAnnotation
方法很基本,没什么好看的。
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if let annotation = annotation as? PKDriverAnnotation
{
let identifier = "driver"
var annotationView: PKDriverAnnotationView
if let dequeuedView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) { // 2
dequeuedView.annotation = annotation
annotationView = dequeuedView as! PKDriverAnnotationView
} else {
// 3
annotationView = PKDriverAnnotationView(annotation: annotation, reuseIdentifier: identifier)
annotationView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(PKTransactionMapViewController.annotationViewTapped(recognizer:))))
}
self.driverAnnotationView = annotationView
return annotationView
} else if let annotation = annotation as? PKAnnotation {
let identifier = "pin"
var view: MKAnnotationView
if let dequeuedView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) { // 2
dequeuedView.annotation = annotation
view = dequeuedView
} else {
// 3
view = MKAnnotationView(annotation: annotation, reuseIdentifier: identifier)
view.image = UIImage(named: "TransactionAnnotation")
view.canShowCallout = false
view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(PKHomeViewController.annotationViewTapped(recognizer:))))
let profilePic = FBSDKProfilePictureView(frame: CGRect(x: 4, y: 4, width: 43, height: 43))
profilePic.center = CGPoint(x: view.bounds.midX, y: profilePic.center.y)
profilePic.profileID = self.transaction!.availableParking.holder.fbid
profilePic.layer.cornerRadius = 21.0
profilePic.clipsToBounds = true
view.addSubview(profilePic)
}
return view
}
return nil
}
我怀疑这与锚点有些关系,我能够修复停车针注释视图"通过view.layer.anchorPoint = CGPoint(x: 0.5, y: 1.0)
轮换,但没有运气驾驶员注释视图(与汽车一起)
感谢帮助SO!
答案 0 :(得分:1)
您应该设置MKAnnotationView
的{{3}}。正如文档所说:
默认情况下,注释视图的中心点位于关联注释的坐标点。您可以根据需要使用此属性重新定位注释视图。该x和y偏移值以点为单位测量。正偏移值向下和向右移动注释视图,而负值向上和向左移动。