ios 检测标注附件控件仅点击右侧的点击标注附件视图

2ic8powd  于 2022-11-19  发布在  iOS
关注(0)|答案(2)|浏览(119)

当我点击注解视图和this behavior it's right时,也会调用我的calloutAccessoryControlTapped。但是,我如何检测用户是否点击了右侧附件视图(在我的情况下是详细信息显示按钮)而不仅仅是在视图中?
我添加了一个简单的检查,但它不起作用。

import UIKit
import MapKit

extension MapVC: MKMapViewDelegate, CLLocationManagerDelegate
{    
    func mapView(mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl)
    {
        if control == view.rightCalloutAccessoryView
        {
            ... // enter here even if I tapped on the view annotation and not on button
        }
    }

}
mu0hgdu0

mu0hgdu01#

要实现这一点,您需要为右侧附件视图添加目标。您可以通过将按钮设置为rightCalloutAccessoryView来实现这一点,如代码片段所示。

class MapViewController: UIViewController, MKMapViewDelegate {

    func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
        if annotation is Annotation {
            let annotationView = AnnotationView(annotation: annotation, reuseIdentifier: "reuseIdentifier")
            let rightButton = UIButton(type: .DetailDisclosure)
            rightButton.addTarget(self, action: #selector(didClickDetailDisclosure(_:)), forControlEvents: .TouchUpInside)
            annotationView.rightCalloutAccessoryView = rightButton
        }
        return nil
    }

    func didClickDetailDisclosure(button: UIButton) {
        // TODO: Perform action when was clicked on right callout accessory view.
    }
}

// Helper classes.
class Annotation: NSObject, MKAnnotation {
    var coordinate: CLLocationCoordinate2D
    var title: String?
    var subtitle: String?

    init(coordinate: CLLocationCoordinate2D, title: String, subtitle: String) {
        self.coordinate = coordinate
        self.title = title
        self.subtitle = subtitle
    }
}

class AnnotationView: MKAnnotationView {

}
jc3wubiy

jc3wubiy2#

1.使用UIView和UITapGestureRecognizer而不是UIControl

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
  let annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "reuseIdentifier")
  let gestureView = UIView(frame:CGRect(x: 0,y: 0,width: 20,height: 20))
  let gestureRecognizer = UITapGestureRecognizer()
  gestureRecognizer.addTarget(self, action:  #selector(MapViewController.didClickGestureRecognizer(_:)))

  gestureView.addGestureRecognizer(gestureRecognizer)
  gestureView.backgroundColor = UIColor.redColor()
  annotationView.rightCalloutAccessoryView = gestureView
  return annotationView
}    

func didClickGestureRecognizer(sender:UITapGestureRecognizer) -> Void {
  print("didClickGestureRecognizer")
}

当您单击rightCalloutAccessoryView时,只有didClickGestureRecognizer会被调用,但是您的calloutAccessoryControlTapped不能被任何人调用。
2.如果您的UI控件为rightCalloutAccessoryView,则可以直接点击MKAnnotationView。否则无法点击MKAnnotationView。
当您点击rightCalloutAccessoryView或直接点击MKAnnotationView时,将调用您的选择器和calloutAccessoryControlTapped
3.如果您有一个UIControl leftCalloutAccessoryView,当您点击它时,您的选择器和calloutAccessoryControlTapped都将被调用。
4.从iOS 9开始,您可以在MKAnnotationView中添加detailCalloutAccessoryView。当您点击它时,只有您的选择器会被调用。
5.您还可以创建自己的自定义MKAnnotationView,并更改其行为。

相关问题