簡體   English   中英

MKAnnotationView子視圖上的UITapGestureRecognizer無法正常工作。 迅速

[英]UITapGestureRecognizer on subview of MKAnnotationView not working. SWIFT

我有一個帶有圖像子視圖的MKAnnotationView

我嘗試向其中添加UITapGestureRecognizer ,但是沒有響應

var tapGestureRecognizer = UITapGestureRecognizer(target: self, action: "annotationClicked:")                    
                imageView.userInteractionEnabled = true
                imageView.addGestureRecognizer(tapGestureRecognizer)
                pinView.addSubview(imageView)
                pinView.bringSubviewToFront(imageView)

恐怕我不知道為什么

要對注解視圖( leftCalloutAccessoryViewrightCalloutAccessoryView )的點擊作出反應,必須將視圖創建為UIControl的后代。 然后,您可以實現MKMapViewDelegate協議的calloutAccessoryControlTapped方法。 無需使用手勢識別器。

以下是一段代碼片段,該代碼將一個按鈕作為標注附件添加到pinAnnotation:

func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
    if annotation is PinAnnotation {
        let pinAnnotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "myPin")

        pinAnnotationView.pinColor = .Purple
        pinAnnotationView.draggable = true
        pinAnnotationView.canShowCallout = true
        pinAnnotationView.animatesDrop = true

        // button as callout accessory
        let deleteButton = UIButton.buttonWithType(UIButtonType.Custom) as UIButton
        deleteButton.frame.size.width = 44
        deleteButton.frame.size.height = 44
        deleteButton.backgroundColor = UIColor.redColor()
        deleteButton.setImage(UIImage(named: "trash"), forState: .Normal)

        pinAnnotationView.leftCalloutAccessoryView = deleteButton

        return pinAnnotationView
    }

    return nil
}

func mapView(mapView: MKMapView!, annotationView view: MKAnnotationView!, calloutAccessoryControlTapped control: UIControl!) {
    if let annotation = view.annotation as? PinAnnotation {
        self.mapView.removeAnnotation(annotation)
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM