如何使用Swift在mapkit上更改未选择的注释pin图像

如何使用Swift在mapkit上更改未选择的注释pin图像,swift,annotations,mapkit,Swift,Annotations,Mapkit,我有一张地图,在这张地图上,我有10个自定义的注释别针。所有管脚都具有相同的自定义图像。当我点击一个别针时,我需要更改所有其他9个注释的图像。可以更改单击的pin的图像,但我需要保持原样,并且需要更改所有其他pin图像 我尝试使用MapmapView.annotations获取所有批注,并尝试查找所选批注和更改其他图像,但无法管理它。你知道怎么做吗 提前感谢。遵守MKMapViewDelegate协议,然后: func mapView(mapView: MKMapView!, didSelect

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

我尝试使用Map
mapView.annotations获取所有批注,并尝试查找所选批注和更改其他图像,但无法管理它。你知道怎么做吗


提前感谢。

遵守
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
        }
    }

此外,如果您想在另一时刻处理所有批注,您还可以保存所选批注供以后在此处使用。

遵循
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
        }
    }
另外,如果您想在以后处理所有批注,也可以将所选批注保存在此处供以后使用。

最终管理:)以一种有点困难的方式解决了问题,但工作顺利:)谢谢您的提示(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为false,因此其他管脚将使用另一个imageview重新绘制,所以这正是我想要做的

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

我用了一个布尔来识别

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为false,因此其他管脚将使用另一个imageview重新绘制,所以这正是我想要做的

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

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

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

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

我的评论太长了,所以我加了它作为回答,你能帮我检查一下我的帖子吗。感谢you@marmaralone我肯定会在晚上尝试你的代码,现在我的工作太忙了:(解决了问题并解释了我是如何做的,作为一个新的答案,谢谢你的提示;)@marmaralone恭喜你解决了!有时候有必要集思广益,因为只有你知道它最后应该是什么样子。请你为目标c写这篇文章好吗?我的评论太长了,所以我把它作为答案加了进去,你能帮我看看我的帖子吗。感谢you@marmaralone我肯定会在晚上尝试你的代码,现在我的工作太忙了:(解决了问题并解释了我是如何做的,作为一个新的答案,谢谢你的提示;)@marmaralone恭喜你解决了!有时候,有必要集思广益,因为只有你才知道它最后应该是什么样子。请你也为目标c写这篇文章,好吗