xamarin.ios - Xamarin Forms 4.1 CalloutAccessoryControlTapped 不再需要点击注释视图
问题描述
最近,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)
{
// ...
}
解决方案
您可以在其中添加自定义aUITapGestureRecognizer
以View
达到效果,以下是步骤:
首先,ges
在 中定义 a CustomMapRenderer
:
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;
}
}
参考:定制销
推荐阅读
- objective-c - iOS 设备屏幕尺寸
- jvm - "-XX:+PrintHeapAtGC" 为什么我在 oracle jdk 文档中找不到这个 jvm 参数?
- python - 熊猫为每个时间箱分配组号
- swiftui - 使用 SwiftUI 等高子视图
- python - 如何通过基于 previos 列添加新列来迭代和计算成行?
- google-apps-script - 验证 Google App Script 中的特定字符串
- android - 指南针和我的位置按钮在地图上的位置颤动
- jboss - jboss_cli 活动会话详细信息
- php - 当用户在 Laravel 中更新电子邮件地址时发送电子邮件激活(确认)链接
- java - 使用带有限定符的相同接口弹出太多构造函数参数