2015-05-29 69 views
4

我正在爲mapview實現自定義標註視圖。我得到這個輸出MKMapView的Swift自定義標註

enter image description here

對於註解視圖我使用

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

    let reuseId = "mapPin" 
    var anView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? CustomCalloutView 
    if anView == nil { 
    anView=CustomCalloutView(annotation: annotation,reuseIdentifier:reuseId) 
     anView!.image = UIImage(named:"mappin.png") 
     let button : UIButton = UIButton.buttonWithType(UIButtonType.DetailDisclosure) as! UIButton 
     button.addTarget(self, action: "buttonClicked:", forControlEvents: UIControlEvents.TouchUpInside) 
     anView!.rightCalloutAccessoryView=button 
    //anView!.addSubview(viewC) 
    mapViewControl.addAnnotation(annotation) 
    } 
    else { 
     anView!.annotation = annotation 
    } 

    return anView 
} 

,並彈出標註視圖

func mapView(mapView: MKMapView!, didSelectAnnotationView view: MKAnnotationView!){ 
    var viewC:UIView=UIView(frame: CGRectMake(0, 0, 50, 50)) 
    viewC.backgroundColor = UIColor.blackColor() 

    view.addSubview(viewC) 
    viewC.center = CGPointMake(viewC.bounds.size.width*0.1, -viewC.bounds.size.height*0.5) 


} 
+0

爲什麼有downvote? – Poonam

+2

我沒有downvote,但也許這是因爲你沒有說明問題是什麼。順便說一下:在viewForAnnotation中,你爲什麼要調用addAnnotation - 這沒有任何意義。另外,使用calloutAccessoryControlTapped委託方法代替rightCalloutAccessoryView的addTarget。 – Anna

+1

下一次更直接與您的問題。但這實際上是一個很好的實現:) – Michael

回答

1

我得到了解決。它的變化非常小。在viewForAnnotation隱藏基本標註如下,

anView!.canShowCallout = false