MKMapView 检测左或右
MKMapView detecting left or right
iOS 11.x Swift 4.0
了解 mapview 并使用此代码创建了一个带有左右 accessoryview 的图钉。
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if annotation is MKUserLocation {
return nil
}
let reuseId = "pin"
var pav:MKPinAnnotationView?
if (pav == nil)
{
pav = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
pav?.isDraggable = true
pav?.canShowCallout = true;
pav?.rightCalloutAccessoryView = UIButton(type: .infoLight)
pav?.leftCalloutAccessoryView = UIButton(type: .contactAdd)
}
else
{
pav?.annotation = annotation;
}
return pav;
}
使用此调用检测何时按下 infoLight and/or contactAdd UIButtons。但我很难弄清楚如何分辨按下了哪个?这个电话火了?但是怎么判断是左手还是右手呢?
func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
print("go figure annotationView \(view)")
if (view.rightCalloutAccessoryView != nil) {
print("right \(view.rightCalloutAccessoryView)")
}
}
显然这是错误的,但是如何知道点击的是左边还是右边?
您可以将控件转换为 UIButton
并检查其类型
func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
let btn = control as! UIButton
if(btn.buttonType == .infoLight)
{
// right Accessory
}
else
{
// left Accessory
}
}
试试下面的代码:
func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
if view.rightCalloutAccessoryView == control {
//right accessory
} else {
// left Accessory
}
}
iOS 11.x Swift 4.0
了解 mapview 并使用此代码创建了一个带有左右 accessoryview 的图钉。
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if annotation is MKUserLocation {
return nil
}
let reuseId = "pin"
var pav:MKPinAnnotationView?
if (pav == nil)
{
pav = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
pav?.isDraggable = true
pav?.canShowCallout = true;
pav?.rightCalloutAccessoryView = UIButton(type: .infoLight)
pav?.leftCalloutAccessoryView = UIButton(type: .contactAdd)
}
else
{
pav?.annotation = annotation;
}
return pav;
}
使用此调用检测何时按下 infoLight and/or contactAdd UIButtons。但我很难弄清楚如何分辨按下了哪个?这个电话火了?但是怎么判断是左手还是右手呢?
func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
print("go figure annotationView \(view)")
if (view.rightCalloutAccessoryView != nil) {
print("right \(view.rightCalloutAccessoryView)")
}
}
显然这是错误的,但是如何知道点击的是左边还是右边?
您可以将控件转换为 UIButton
并检查其类型
func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
let btn = control as! UIButton
if(btn.buttonType == .infoLight)
{
// right Accessory
}
else
{
// left Accessory
}
}
试试下面的代码:
func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
if view.rightCalloutAccessoryView == control {
//right accessory
} else {
// left Accessory
}
}