How to change non selected annotation pin images on mapkit with Swift

巧了我就是萌 提交于 2019-12-08 06:33:46

问题


I have a map and on this map I have 10 custom annotation pins. All pins have same custom image. When i click on a pin, i need to change all other 9 annotation's images. its possible to change clicked pin's image but i need to keep as it is and i need to change all other pins images.

I tried to get all annotations with Map mapView.annotations and tried to find selected annotation and change others images but couldnt manage it. And idea how to do it?

Thanks in advice


回答1:


Conform to MKMapViewDelegate protocol and then:

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

Also you can save the selected annotation for later use here, if you want to process all annotations in another moment.




回答2:


finally managed :) solved the problem little bit hard way but working smooth :) thank you for the tip rshev ;)

i used a bool for tap recognize

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

and when pin tapped ->

  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")

i removed all pins without selected pin, and then draw them back but this time tapcontrol is false so other pins are redrawed with another imageview, so thats what i exactly want to do.




回答3:


You just have to overrride isSelected property inside your MKAnnotationView subclass.

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


来源:https://stackoverflow.com/questions/31934110/how-to-change-non-selected-annotation-pin-images-on-mapkit-with-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!