我在Monotouch中有以下情况。我已经实现了TappCandy在以下帖子How to customize the callout bubble for MKAnnotationView?中显示的精彩解决方案。我的自定义标注视图是MKPinAnnotationView子类。
到目前为止,该解决方案在标注显示方面运行良好。但是我对callout子视图的用户交互有问题。它包括三个标签和一个按钮,每个标签位于另一个下方。该按钮定义了一个TouchUpInside事件,但似乎按钮中没有启用用户交互。可识别的按钮的唯一触摸区域是针图像上方的小区域。如果子视图关于引脚图像的偏移高于阈值,则看起来该按钮没有触摸区域。
所以我的问题是:我如何分别为按钮和整个子视图进行正常的用户交互?我执行自定义注释视图类的SetSelected时用于添加callout子视图的代码如下:
public override void SetSelected(bool selected, bool animated)
{
base.SetSelected(selected, animated);
if(selected)
{
ShowCallout(); // this.AddSubview(calloutView);
}
else
{
FadeOutCallout(); // calloutView.RemoveFromSuperview();
}
}
其中ShowCallout()和FadeCallout()通过淡入/淡出效果添加和删除自定义标注子视图(带有三个标签和按钮)。
创建自定义注释视图时创建自定义标注子视图并将其放置在引脚上方的特定偏移中的代码如下:
private void CreateCalloutView()
{
calloutView = new UIView();
calloutView.AutoresizingMask = UIViewAutoresizing.All;
calloutViewFrame = new RectangleF(calloutViewFrame.X + offsetFromParent.X,
calloutViewFrame.Y + offsetFromParent.Y,
calloutViewFrame.Size.Width,
calloutViewFrame.Size.Height);
calloutView.Frame = calloutViewFrame;
calloutView.BackgroundColor = UIColor.Clear;
// Create the callout label views.
text1 = new UILabel();
text1.AutoresizingMask = UIViewAutoresizing.All;
text1.TextAlignment = UITextAlignment.Center;
text1.Frame = new RectangleF(0, 0, calloutView.Frame.Size.Width, 21);
text1.Text = "text1";
text1.BackgroundColor = UIColor.Clear;
text.Font = UIFont.SystemFontOfSize(17);
text2 = new UILabel();
text2.AutoresizingMask = UIViewAutoresizing.All;
text2.TextAlignment = UITextAlignment.Center;
text2.Frame = new RectangleF(0, text1.Frame.Y +
text1.Frame.Size.Height,
calloutView.Frame.Size.Width, 21);
text2.Text = "text2";
text2.BackgroundColor = UIColor.Clear;
text2.Font = UIFont.SystemFontOfSize(17);
text3 = new UILabel();
text3.AutoresizingMask = UIViewAutoresizing.All;
text3.TextAlignment = UITextAlignment.Center;
text3.Frame = new RectangleF(0, text1.Frame.Y +
text1.Frame.Size.Height +
text2.Frame.Size.Height,
calloutView.Frame.Size.Width, 21);
text3.Text = "text3";
text3.BackgroundColor = UIColor.Clear;
text3.Font = UIFont.SystemFontOfSize(17);
button = UIButton.FromType(UIButtonType.RoundedRect);
button.UserInteractionEnabled = true;
button.AutoresizingMask = UIViewAutoresizing.All;
button.Frame = new RectangleF(20, text1.Frame.Y +
text1.Frame.Size.Height +
text2.Frame.Size.Height +
text3.Frame.Size.Height, 156, 37);
button.Font = UIFont.SystemFontOfSize(15);
button.SetTitleColor(UIColor.Black, UIControlState.Normal);
button.SetTitle("Button >", UIControlState.Normal);
button.TouchUpInside += (sender, e) => {
Console.WriteLine("Button is touched");
};
calloutView.AddSubview(text1);
calloutView.AddSubview(text2);
calloutView.AddSubview(text3);
calloutView.AddSubview(button);
}
事实证明,标注视图显示得很好但我无法触摸按钮。
更新:我注意到只有当按钮与下方的图钉图像重叠时,该按钮才可以点击。如果我配置子视图的偏移量使按钮正好位于图钉图像上,那么它肯定是可点击的。
我还注意到注释视图的EnableInputClicksWhenVisible属性上存在MonoTouch.Foundation.MonoTouchException。我不知道这是否与任何事情有关,但我觉得这是问题的根源。