Xamarin Forms 4.1 CalloutAccessoryControlTapped 不再需要点击注释视图
Xamarin Forms 4.1 CalloutAccessoryControlTapped is no more called for tap on annotation view
最近,iOS 地图注释中的一个行为发生了变化:
当用户点击注释视图时,事件 CalloutAccessoryControlTapped
不再被调用。例如,如果我点击上图中的红色区域。触发事件的唯一方法是点击右侧的信息按钮。
有没有办法在我们点击注释的整个表面时强制 CalloutAccessoryControlTapped
上升?
protected override void OnElementChanged(ElementChangedEventArgs<View> e)
{
base.OnElementChanged(e);
if (Control is MKMapView nativeMap)
{
if (e.OldElement != null)
{
nativeMap.RemoveAnnotations(nativeMap.Annotations);
nativeMap.GetViewForAnnotation = null;
nativeMap.CalloutAccessoryControlTapped -= OnCalloutAccessoryControlTapped;
}
if (e.NewElement != null)
{
CustomMap = (CustomMap)e.NewElement;
nativeMap.GetViewForAnnotation = GetViewForAnnotation;
nativeMap.CalloutAccessoryControlTapped += OnCalloutAccessoryControlTapped;
}
}
}
// event delegate
private void OnCalloutAccessoryControlTapped(object sender, MKMapViewAccessoryTappedEventArgs e)
{
// ...
}
你可以在那个View
中添加一个自定义的UITapGestureRecognizer
来达到效果,步骤如下:
首先,在CustomMapRenderer
中定义一个ges
:
public class CustomMapRenderer : MapRenderer
{
UIView customPinView;
List<CustomPin> customPins;
UITapGestureRecognizer ges;
...
}
然后在 OnDidSelectAnnotationView
中,将 ges
添加到 customView
:
void OnDidSelectAnnotationView(object sender, MKAnnotationViewEventArgs e)
{
var customView = e.View as CustomMKAnnotationView;
customPinView = new UIView();
Action action = () => {
if (!string.IsNullOrWhiteSpace(((CustomMKAnnotationView)customView).Url))
{
UIApplication.SharedApplication.OpenUrl(new Foundation.NSUrl(((CustomMKAnnotationView)customView).Url));
}
};
ges = new UITapGestureRecognizer(action);
customView.AddGestureRecognizer(ges);
if (customView.MarkerId == "Xamarin")
{
customPinView.Frame = new CGRect(0, 0, 200, 84);
var image = new UIImageView(new CGRect(0, 0, 200, 84));
image.Image = UIImage.FromFile("xamarin.png");
customPinView.AddSubview(image);
customPinView.Center = new CGPoint(0, -(e.View.Frame.Height + 75));
e.View.AddSubview(customPinView);
}
}
并在 OnDidDeselectAnnotationView
删除它:
void OnDidDeselectAnnotationView(object sender, MKAnnotationViewEventArgs e)
{
var customView = e.View as CustomMKAnnotationView;
customView.RemoveGestureRecognizer(ges);
if (!e.View.Selected)
{
customPinView.RemoveFromSuperview();
customPinView.Dispose();
customPinView = null;
}
}
最近,iOS 地图注释中的一个行为发生了变化:
当用户点击注释视图时,事件 CalloutAccessoryControlTapped
不再被调用。例如,如果我点击上图中的红色区域。触发事件的唯一方法是点击右侧的信息按钮。
有没有办法在我们点击注释的整个表面时强制 CalloutAccessoryControlTapped
上升?
protected override void OnElementChanged(ElementChangedEventArgs<View> e)
{
base.OnElementChanged(e);
if (Control is MKMapView nativeMap)
{
if (e.OldElement != null)
{
nativeMap.RemoveAnnotations(nativeMap.Annotations);
nativeMap.GetViewForAnnotation = null;
nativeMap.CalloutAccessoryControlTapped -= OnCalloutAccessoryControlTapped;
}
if (e.NewElement != null)
{
CustomMap = (CustomMap)e.NewElement;
nativeMap.GetViewForAnnotation = GetViewForAnnotation;
nativeMap.CalloutAccessoryControlTapped += OnCalloutAccessoryControlTapped;
}
}
}
// event delegate
private void OnCalloutAccessoryControlTapped(object sender, MKMapViewAccessoryTappedEventArgs e)
{
// ...
}
你可以在那个View
中添加一个自定义的UITapGestureRecognizer
来达到效果,步骤如下:
首先,在CustomMapRenderer
中定义一个ges
:
public class CustomMapRenderer : MapRenderer
{
UIView customPinView;
List<CustomPin> customPins;
UITapGestureRecognizer ges;
...
}
然后在 OnDidSelectAnnotationView
中,将 ges
添加到 customView
:
void OnDidSelectAnnotationView(object sender, MKAnnotationViewEventArgs e)
{
var customView = e.View as CustomMKAnnotationView;
customPinView = new UIView();
Action action = () => {
if (!string.IsNullOrWhiteSpace(((CustomMKAnnotationView)customView).Url))
{
UIApplication.SharedApplication.OpenUrl(new Foundation.NSUrl(((CustomMKAnnotationView)customView).Url));
}
};
ges = new UITapGestureRecognizer(action);
customView.AddGestureRecognizer(ges);
if (customView.MarkerId == "Xamarin")
{
customPinView.Frame = new CGRect(0, 0, 200, 84);
var image = new UIImageView(new CGRect(0, 0, 200, 84));
image.Image = UIImage.FromFile("xamarin.png");
customPinView.AddSubview(image);
customPinView.Center = new CGPoint(0, -(e.View.Frame.Height + 75));
e.View.AddSubview(customPinView);
}
}
并在 OnDidDeselectAnnotationView
删除它:
void OnDidDeselectAnnotationView(object sender, MKAnnotationViewEventArgs e)
{
var customView = e.View as CustomMKAnnotationView;
customView.RemoveGestureRecognizer(ges);
if (!e.View.Selected)
{
customPinView.RemoveFromSuperview();
customPinView.Dispose();
customPinView = null;
}
}