在 calloutAccessoryControlTapped 上检测仅点击 rightCalloutAccessoryView
Detect on calloutAccessoryControlTapped only the tap on rightCalloutAccessoryView
我的 calloutAccessoryControlTapped
也被调用,当我点击注释视图和 this behavior it's right 时。但是我如何检测用户是否点击了正确的附件视图(在我的例子中是详细信息披露按钮)而不仅仅是在视图中?
我添加了一个简单的检查,但它不起作用。
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
}
}
}
要实现它,您需要为正确的附件视图添加目标。您可以通过将按钮设置为 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 {
}
使用 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.If你有一个UIControl为rightCalloutAccessoryView
,你可以直接点击MKAnnotationView.Otherwise MKAnnotationView无法点击
当您点击 rightCalloutAccessoryView
或直接点击 MKAnnotationView
时,您的选择器和 calloutAccessoryControlTapped
都会被调用
3.If 你有一个 UIControl 作为 leftCalloutAccessoryView
,当你点击它时你的选择器和 calloutAccessoryControlTapped
都会被调用。
4.Since iOS 9,你可以在你的 MKAnnotationView.Only 中有一个 detailCalloutAccessoryView
当你点击它时你的选择器将被调用。
5.Also您可以创建自己的自定义 MKAnnotationView,并更改其行为。
我的 calloutAccessoryControlTapped
也被调用,当我点击注释视图和 this behavior it's right 时。但是我如何检测用户是否点击了正确的附件视图(在我的例子中是详细信息披露按钮)而不仅仅是在视图中?
我添加了一个简单的检查,但它不起作用。
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
}
}
}
要实现它,您需要为正确的附件视图添加目标。您可以通过将按钮设置为 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 {
}
使用 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.If你有一个UIControl为rightCalloutAccessoryView
,你可以直接点击MKAnnotationView.Otherwise MKAnnotationView无法点击
当您点击 rightCalloutAccessoryView
或直接点击 MKAnnotationView
calloutAccessoryControlTapped
都会被调用
3.If 你有一个 UIControl 作为 leftCalloutAccessoryView
,当你点击它时你的选择器和 calloutAccessoryControlTapped
都会被调用。
4.Since iOS 9,你可以在你的 MKAnnotationView.Only 中有一个 detailCalloutAccessoryView
当你点击它时你的选择器将被调用。
5.Also您可以创建自己的自定义 MKAnnotationView,并更改其行为。