gpt4 book ai didi

ios - 将图像添加到 MKAnnotationView 会取代 pin

转载 作者:可可西里 更新时间:2023-11-01 00:38:13 25 4
gpt4 key购买 nike

我试图用图像放置一些代表公交车站的大头针,当我为图像添加广告时,它会改变大头针的位置。当我没有设置图像时,图钉会掉落在正确的位置。

func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {

if annotation is StopAnnotation {

let identifier = "stopAnnotation"
var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier)
if pinView == nil {
//println("Pinview was nil")
pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
pinView!.canShowCallout = true
pinView.image = UIImage(named: "stopIcon")
}
return pinView

}
return nil
}

例子 enter image description here enter image description here

我尝试使用的图像: enter image description here

谁能告诉我为什么要这样做?我在此应用程序的 Obj-C 版本中使用完全相同的图像,一切正常。

最佳答案

代码正在创建一个带有自定义图像的 MKPinAnnotationView

MKPinAnnotationView 类应该只用于显示默认图钉图像。

要显示自定义图像,最好使用普通的 MKAnnotationView


因为代码使用了 MKPinAnnotationView,所以图像会自动获得一个应用于它的偏移量(centerOffset 属性)。

此内置偏移适用于默认图钉图像,但不适用于您的自定义图像。

与其尝试覆盖此默认行为,不如使用普通的 MKAnnotationView:

func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {

if annotation is StopAnnotation {

let identifier = "stopAnnotation"
var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier)
if pinView == nil {
//println("Pinview was nil")

//Create a plain MKAnnotationView if using a custom image...
pinView = MKAnnotationView(annotation: annotation, reuseIdentifier: identifier)

pinView!.canShowCallout = true
pinView.image = UIImage(named: "stopIcon")
}
else {
//Unrelated to the image problem but...
//Update the annotation reference if re-using a view...
pinView.annotation = annotation
}

return pinView
}
return nil
}

关于ios - 将图像添加到 MKAnnotationView 会取代 pin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25799174/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com