gpt4 book ai didi

swift - viewForAnnotation 有时无法获取自定义 MKAnnotation 类并正确呈现它

转载 作者:行者123 更新时间:2023-11-28 05:30:16 27 4
gpt4 key购买 nike

我有不同的类(MKAnnotation 子类)代表 map 注释。
我将它们加载到 map 上,一切正常,但是当我移动或缩放 map 图钉时,它们的图像开始丢失。
当我平移 map viewForAnnotation 被调用时,例如 BluePin 类的 pin 不再进入它的 if block ,结果它呈现来自 viewForAnnotation 的最后一个注释,带有默认图钉图像(绿色)。

更新

我刚刚意识到代码实际上正确地进入了每个 IF 但从未进入嵌套 IF 所以这行代码会产生一些问题:

var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier("blue")
if (annotationView == nil) { ... after zoom/pan never get here }...

这里可能是什么问题:

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

if annotation is MKUserLocation {
return nil
}
if(annotation.isKindOfClass(BluePin)){

var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier("blue")
if (annotationView == nil) {
annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "blue")
annotationView.canShowCallout = true;

annotationView.image = UIImage(named: "blue")

return annotationView
}
} else if(annotation.isKindOfClass(RedPin)){

var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier("red")
if (annotationView == nil) {
annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "red")
annotationView.canShowCallout = true;
annotationView.image = UIImage(named: "red")

return annotationView
}
}


var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier("def") as? MKPinAnnotationView
if pinView == nil {
pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "def")
pinView!.pinColor = .Green

}
else {
pinView!.annotation = annotation
}

return pinView
}

最佳答案

dequeueReusableAnnotationViewWithIdentifier 方法将返回一个以前创建的 View (如果有的话),但当前未用于显示(这是使用此方法的全部意义,以便您可以重用 View ).

当该方法确实找到可以重复使用的 View 时,它将返回该 View ,因此结果将为非nil

当您缩放/平移 map 时,一些注释会从 View 中消失,而新的注释会出现。进入 View 的那些现在可以重新使用不再可见的注释 View 。所以 dequeueReusableAnnotationViewWithIdentifier 返回一个非 nil 结果。

当前代码不处理 dequeueReusableAnnotationViewWithIdentifier 为蓝色和红色引脚返回非 nil 的情况,因此 执行继续执行之后的下一个语句大的 if block var pinView = mapView... 然后创建默认的绿色图钉 View 。

需要修改代码以处理 dequeueReusableAnnotationViewWithIdentifier 为蓝色和红色引脚返回非 nil 的情况:

    if(annotation.isKindOfClass(BluePin)) {
var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier("blue")
if (annotationView == nil) {
annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "blue")
annotationView.canShowCallout = true;
annotationView.image = UIImage(named: "blue")
} else {
//handle blue view re-use...
annotationView.annotation = annotation
}

//move return to after the if-else...
return annotationView
}
else if(annotation.isKindOfClass(RedPin)) {

var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier("red")
if (annotationView == nil) {
annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "red")
annotationView.canShowCallout = true;
annotationView.image = UIImage(named: "red")
} else {
//handle red view re-use...
annotationView.annotation = annotation
}

//move return to after the if-else...
return annotationView
}

关于swift - viewForAnnotation 有时无法获取自定义 MKAnnotation 类并正确呈现它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29049097/

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