swift - 为什么在重用注释 View Mapkit Swift 4 时我的标签文本没有改变

标签 swift mkannotationview

我正在使用 Mapkit 显示某些位置的自定义注释,并在注释上方显示注释名称的标签。我注意到,在重新创建/重用一些注释后,标签中的文本与注释的名称不同。我认为当注释被重用时,标题不会改变。重复使用的注释越多,它们的标题就越错误。

知道为什么标签的标题在重复使用时与注释的标题不匹配吗?

这是我的代码:

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    if annotation is MKUserLocation { return nil }

    var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "pin") as? MKPinAnnotationView

    let annotationLabel = UILabel(frame: CGRect(x: -40, y: -35, width: 105, height: 30))
    annotationLabel.numberOfLines = 3
    annotationLabel.textAlignment = .center
    annotationLabel.font = UIFont(name: "Rockwell", size: 10)

    let strokeTextAttributes: [NSAttributedStringKey: Any] = [
        .strokeColor : UIColor.white,
        .foregroundColor : UIColor.black,
        .strokeWidth : -4,
        ]

    if annotationView == nil {
        annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "pin")
        annotationView?.animatesDrop = true
        annotationView?.canShowCallout = false

        annotationLabel.attributedText = NSMutableAttributedString(string: annotation.title!!, attributes: strokeTextAttributes)
        annotationView?.addSubview(annotationLabel)
    } else {
        annotationView?.annotation = annotation
        annotationLabel.attributedText = NSMutableAttributedString(string: annotation.title!!, attributes: strokeTextAttributes)
    }


    annotationLabel.clipsToBounds = true
    annotationLabel.backgroundColor = UIColor.white
    annotationLabel.layer.cornerRadius = 15

    return annotationView
}

因此,无论哪种方式,标签的文本都应该更改为标签的标题,因为无论是否已创建,我都会将文本更改为当前注释。

以下是一些图片,可以帮助您更好地了解: Annotation before being reused

Annotation after being reused

Actual annotation name

如您所见,“Luna Blu”是蒂伯龙的一家餐厅。在需要更多注释并且重复使用该注释后,它表示“Luna Blu”位于城市中心。为了表明这实际上是不正确的,当我单击注释时,会出现一个滑出菜单,其中包含真实的注释标题,即“泰勒街咖啡店”。

最佳答案

当注释不为 nil 并且控制转到 else 时,标签的标题会更改,但它不是添加到annotationView的标签,而是当 func 返回时将被释放的本地变量

annotationLabel.attributedText = NSMutableAttributedString(string: annotation.title!!, attributes: strokeTextAttributes)

因此您必须在添加标签并将文本分配给它之前通过您提供的标签来查询该标签的 View

//

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    if annotation is MKUserLocation { return nil }

    var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "pin") as? MKPinAnnotationView

    let annotationLabel = UILabel(frame: CGRect(x: -40, y: -35, width: 105, height: 30))
    annotationLabel.numberOfLines = 3
    annotationLabel.textAlignment = .center
    annotationLabel.font = UIFont(name: "Rockwell", size: 10)
    annotationLabel.tag = 22
    let strokeTextAttributes: [NSAttributedStringKey: Any] = [
        .strokeColor : UIColor.white,
        .foregroundColor : UIColor.black,
        .strokeWidth : -4,
        ]

    if annotationView == nil {
        annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "pin")
        annotationView?.animatesDrop = true
        annotationView?.canShowCallout = false

        annotationLabel.attributedText = NSMutableAttributedString(string: annotation.title!!, attributes: strokeTextAttributes)
        annotationView?.addSubview(annotationLabel)
    } else {
        annotationView?.annotation = annotation

        for item in annotationView!.subviews {
           if item.tag == 22 {
               let lbl = item as! UILabel
               lbl.attributedText = NSMutableAttributedString(string: annotation.title!!, attributes: strokeTextAttributes)
               break
           }
        }

    }


    annotationLabel.clipsToBounds = true
    annotationLabel.backgroundColor = UIColor.white
    annotationLabel.layer.cornerRadius = 15

    return annotationView
}

关于swift - 为什么在重用注释 View Mapkit Swift 4 时我的标签文本没有改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50899465/

相关文章:

ios - MKAnnotation 点击​​时没有响应

objective-c - MKAnnotationView被拖拽时如何连续回调

ios - Firebase 登录错误消息未显示

ios - 退出应用程序时从远程通知显示 uiviewcontroller

ios - 如何在 swift 中使用 MapKit 旋转自定义 userLocationAnnotationView 图像?

swift - 在 MKMapView 上更新用户位置的图像

ios - map 注释在应该聚类时重叠

ios - Swift:如何限制 CollectionView 中的 numberOfItemsInSection?

swift - 如何改变 Swift Dictionary of Dictionary

ios - 使用服务器上的 Socket.IO-Client-Swift 和 Swift、Sails.js 订阅房间(套接字)时出现问题