【问题标题】:How to change non selected annotation pin images on mapkit with Swift如何使用 Swift 在 mapkit 上更改未选择的注释图钉图像
【发布时间】:2015-08-11 05:50:29
【问题描述】:

我有一张地图,在这张地图上,我有 10 个自定义注释图钉。所有引脚都具有相同的自定义图像。当我单击一个图钉时,我需要更改所有其他 9 个注释的图像。可以更改单击的图钉的图像,但我需要保持原样,并且我需要更改所有其他图钉图像。

我尝试使用 Map mapView.annotations 获取所有注释,并尝试找到选定的注释并更改其他图像但无法管理它。想知道怎么做?

提前致谢。

【问题讨论】:

    标签: swift annotations mapkit


    【解决方案1】:

    符合MKMapViewDelegate协议然后:

    func mapView(mapView: MKMapView!, didSelectAnnotationView view: MKAnnotationView!) {
        let selectedAnnotation = view.annotation
        for annotation in mapView.annotations {
            if let annotation = annotation as? MKAnnotation where !annotation.isEqual(selectedAnnotation) {
                // do some actions on non-selected annotations in 'annotation' var
            }
        }
    

    如果您想稍后处理所有注释,您也可以在此处保存选定的注释以供以后使用。

    【讨论】:

    • 我的评论太长了,所以我把它添加为答案,你能检查我的帖子吗?谢谢
    • @marmaralone 肯定会在晚上尝试你的代码,现在我的工作太忙了:(
    • 解决了这个问题并解释了我是如何做到的,作为一个新的答案,谢谢你的提示;)
    • @marmaralone 恭喜您解决了!有时需要头脑风暴一下,因为只有你自己才知道它最后应该是什么样子。
    • 请你也为目标c写这个,谢谢
    【解决方案2】:

    终于解决了 :) 解决了这个问题有点困难,但工作顺利 :) 谢谢你的提示 rshev ;)

    我用一个布尔值来识别水龙头

    func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
    
        if annotation is CustomAnnotation {
            var pin = mapView.dequeueReusableAnnotationViewWithIdentifier(customAnnotationViewIdentifier)
            pin = MKPinAnnotationView(annotation: annotation, reuseIdentifier: customAnnotationViewIdentifier)
    
            if tapControl {
                pin.image = UIImage(named: "MapAnnotationIcon")
            } else {
                pin.image = UIImage(named: "SelectedMapAnnotationIcon")
            }
    
            if pin == nil {
                pin.canShowCallout = false
            } else {
                pin.annotation = annotation
            }
    
            return pin
    

    当点击针时 ->

      if let annotation = view.annotation as? CustomAnnotation {
    
            tapControl = !tapControl
            for annotation in mapView.annotations {
                if let annotation = annotation as? MKAnnotation where !annotation.isEqual(selectedAnnotation) {
                    mapView.removeAnnotation(annotation)
                }
            }
            addAnnotations()
            println("tapped")
    

    我删除了所有未选择引脚的引脚,然后将它们拉回来,但这次 tapcontrol 是假的,所以其他引脚用另一个 imageview 重绘,所以这就是我想要做的。

    【讨论】:

      【解决方案3】:

      您只需要在 MKAnnotationView 子类中覆盖 isSelected 属性。

      override var isSelected: Bool {
          didSet {
              if isSelected {
                  // do stuff where annotation is selected
              } else {
                  // do opposite
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2015-12-14
        • 1970-01-01
        • 1970-01-01
        • 2021-11-09
        • 2011-11-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多