2016-01-13 58 views
0

我有自定義視圖,當用戶點擊註釋時出現,但我不知道如何在用戶點擊地圖上的其他位置時使這些視圖消失。 在Google Map SDK for iOS中,它會是didTapAtCoordinate,然後我會設置view.hidden = true如何在用戶使用MapKit在地圖上點擊時執行操作Swift

我認爲可以用touchBegan來完成,雖然我之前沒有使用過這個功能嗎?雖然我更喜歡MapKit版本的Google谷歌didTapAtCoordinate

編輯

class ViewController: UIViewController, MKMapViewDelegate { 

    @IBOutlet weak var mapView: MKMapView! 
    @IBOutlet weak var myCustomView: UIView! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     myCustomView.hidden = true 
     //MyAnnatationClass is my class where I store MKAnnatations with additional details 
     let ann = MyAnnatationClass(coordinate: CLLocationCoordinate2D(latitude: 54.694084, longitude: 25.288782), title: "DEMO2", name: "Some Name") 
     mapView.addAnnotation(ann) 
    } 

    func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? { 

     let identifier = "id" 

     if annotation.isKindOfClass(MyAnnatationClass.self) { 

      var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier) 

      if annotationView == nil { 

       annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: identifier) 
       annotationView?.image = UIImage(named: "pin.png") 
       annotationView!.canShowCallout = true 
      } else { 

       annotationView!.annotation = annotation 
      } 

      return annotationView 
     } 

     return nil 
    } 

    func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) { 
     //Set view details 
     let myAnn = view.annotation as! MyAnnatationClass 
     someViewLabel.text = myAnn.name 
     myCustomView.hidden = false 

     let annotationTap = UITapGestureRecognizer(target: self, action: "tapRecognized:") 
     annotationTap.numberOfTapsRequired = 1 
     view.addGestureRecognizer(annotationTap) 

     let selectedAnnotations = mapView.selectedAnnotations 

     for annotationView in selectedAnnotations{ 
      mapView.deselectAnnotation(annotationView, animated: true) 
     } 
    } 

    func mapView(mapView: MKMapView, didDeselectAnnotationView view: MKAnnotationView) { 
     view.hidden = true 
    } 

    func tapRecognized(gesture:UITapGestureRecognizer){ 

     let selectedAnnotations = mapView.selectedAnnotations 

     for annotationView in selectedAnnotations{ 
      mapView.deselectAnnotation(annotationView, animated: true) 
     } 
    } 
} 

目前,當我觸摸註釋,myCustomView出現和註釋消失。如果在didDeselectAnnotationView中我將view更改爲myCustomView,則自定義視圖不會顯示。

+0

嘗試製作一個布爾標誌,就像我在我的回答中提到的那樣。你可以檢查isShow並根據標誌切換隱藏 – MisterGreen

回答

0

我認爲伊戈爾的答案https://stackoverflow.com/a/11455213/4205432將對你有好處,我只是把它翻譯成swift。你有一個委託方法,你可以用它來知道哪些註解視圖被取消選擇,並在那裏做你的東西。 正如伊戈爾的回答中所提到的,您可以添加一個布爾標誌來標識是否「isShow」。

func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) { 

     let annotationTap = UITapGestureRecognizer(target: self, action: "tapRecognized") 
     annotationTap.numberOfTapsRequired = 1 
     view.addGestureRecognizer(annotationTap) 

     let selectedAnnotations = mapView.selectedAnnotations 

     for annotationView in selectedAnnotations{ 
      mapView.deselectAnnotation(annotationView, animated: true) 
     } 
    } 

    func tapRecognized(gesture:UITapGestureRecognizer){ 

     let selectedAnnotations = mapView.selectedAnnotations 

     for annotationView in selectedAnnotations{ 
      mapView.deselectAnnotation(annotationView, animated: true) 
     } 
    } 

    func mapView(mapView: MKMapView, didDeselectAnnotationView view: MKAnnotationView) { 

     view.hidden = true 
    } 
+0

你還可以翻譯'annotationTap'嗎? 應該是這樣的:'var annotationTap = UITapGestureRecognizer(target:self,action:「tapRecognized:」)' – Xernox

+0

我用註釋點擊編輯答案......希望它能幫助你 – MisterGreen

+0

好吧,現在我的自定義視圖從來沒有出現。在'didSelectAnnotationView'中,我在該視圖中指定了一些標籤,並將'view.hidden'設置爲false – Xernox

0

我建議使用內置的註釋標註機制。您可以創建自定義標註視圖,並且系統會響應用戶點擊註釋來顯示它,並且如果用戶點擊其他位置,則會將其動畫化。

地圖視圖已經廣泛使用手勢,所以試圖在上面添加自己的手勢處理將很困難。

+0

它會好得多使用標註視圖,但在我的情況下 - 自定義視圖應該是大約一半的屏幕尺寸,並且當用戶移動地圖時它應該保持原位。我認爲這是不可能的標註視圖? – Xernox

+0

是的。標註視圖固定在地圖內容上,並在地圖滾動時滾動。 –

相關問題