2017-10-22 14 views
0

내 mapView에 대한 내 주석에 주석이 있습니다. 내 주석에 단추가 있고 사용자가 단추를 클릭 할 때 단추 이미지를 (플러스에서 마이너스로) 변경할 수 있기를 원합니다. 나는 이것을 어디서 갱신 할 수 있을지 확신하지 못한다. 사용자가 핀을 클릭하면 현재 내 버튼 이미지가 설정됩니다. 지도 자체를 다시로드하기 전까지는 이미지 자체가 업데이트되지 않지만 사용자가 클릭 한 즉시 특수 효과보기에서 버튼을 업데이트하려고합니다. 주석을 팝업하는 동안 단추를 업데이트하는 방법을 잘 모르겠습니다.Swift 3.0 핀의 주석 업데이트

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) { 

    if let annotation = view.annotation as? MKPointAnnotation { 
       let btn = UIButton(type: .detailDisclosure) 
       view.rightCalloutAccessoryView = btn 
       if annotation.accessibilityValue! == "Minus" { 

        let button = UIButton(frame: CGRect(x: 0, y: 0, width: 30, height: 30)) 
        button.setBackgroundImage(UIImage(named: "minus"), for: .normal) 
        button.addTarget(self, action: #selector(MapVC.minus), for: .touchUpInside) 
        view.leftCalloutAccessoryView = button 
       } else { 
        let button = UIButton(frame: CGRect(x: 0, y: 0, width: 30, height: 30)) 
        button.setBackgroundImage(UIImage(named: "plus"), for: .normal) 
        button.addTarget(self, action: #selector(MapVC.plus), for: .touchUpInside) 
        view.leftCalloutAccessoryView = button 
       } 
    } 
} 

답변

1

didSelect 대리자 메서드를 사용할 필요가 없습니다. 사용자가 버튼을 클릭하면 calloutAccessoryControlTapped 대리자 메서드가 호출됩니다. 이 위임 메서드에서 단추 이미지를 변경할 수 있습니다.

func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) { 
    if control == view.leftCalloutAccessoryView { 
     let button = control as! UIButton 
     if annotation.accessibilityValue! == "Minus" { 
      button.setBackgroundImage(UIImage(named: "minus"), for: .normal) 
     } else { 
      button.setBackgroundImage(UIImage(named: "plus"), for: .normal) 
     } 
    } 
} 
+0

이 대리인의 핀 주석 색도 변경할 수 있습니까? – Kevin

+0

예, 가능합니다. '핀 =보기! MKPinAnnotationView; pin.pinTintColor = .blue' –