如何自定义MKAnnotationView的标注气泡? [英] How to customize the callout bubble for MKAnnotationView?
问题描述
我目前正在使用mapkit并且卡住了。
I'm currently working with the mapkit and am stuck.
我有一个我正在使用的自定义注释视图,我想使用image属性来显示我自己的图标在地图上的点。我有这个工作正常。但我还想做的是覆盖默认的标注视图(触摸注释图标时显示标题/副标题的气泡)。我希望能够控制callout本身:mapkit只提供对左右辅助标注视图的访问,但无法为标注气泡提供自定义视图,或者无法为其提供零大小或其他任何内容。
I have a custom annotation view I am using, and I want to use the image property to display the point on the map with my own icon. I have this working fine. But what I would also like to do is to override the default callout view (the bubble that shows up with the title/subtitle when the annotation icon is touched). I want to be able to control the callout itself: the mapkit only provides access to the left and right ancillary callout views, but no way to provide a custom view for the callout bubble, or to give it zero size, or anything else.
我的想法是覆盖我的 MKMapViewDelegate
中的selectAnnotation / deselectAnnotation,然后通过拨打电话来绘制我自己的自定义视图到我的自定义注释视图。这是有效的,但只有在我的自定义注释视图类中将 canShowCallout
设置为 YES
时。如果我将此设置为否
(这就是我想要的,那么不会调用这些方法,因此不会绘制默认的标注气泡)。所以我无法知道用户是否在地图上触摸了我的点(选中它)或者触摸了一个不属于我的注释视图(被选中)的点而没有显示默认的标注气泡视图。
My idea was to override selectAnnotation/deselectAnnotation in my MKMapViewDelegate
, and then draw my own custom view by making a call to my custom annotation view. This works, but only when canShowCallout
is set to YES
in my custom annotation view class. These methods are NOT called if I have this set to NO
(which is what I want, so that the default callout bubble is not drawn). So I have no way of knowing if the user touched on my point on the map (selected it) or touched a point that is not part of my annotation views (delected it) without having the default callout bubble view show up.
我试着走另一条路,只是自己在地图上处理所有的触摸事件,我似乎无法让这个工作。我在地图视图中阅读了与捕捉触摸事件相关的其他帖子,但它们并不完全是我想要的。有没有办法挖掘地图视图以在绘制之前删除标注气泡?我很茫然。
I tried going down a different path and just handling all touch events myself in the map, and I can't seem to get this working. I read other posts related to catching touch events in the map view, but they aren't exactly what I want. Is there a way to dig into the map view to remove the callout bubble before drawing? I'm at a loss.
有什么建议吗?我错过了一些明显的东西吗?
Any suggestions? Am I missing something obvious?
推荐答案
有一个更简单的解决方案。
There is an even easier solution.
创建自定义 UIView
(针对您的标注)。
Create a custom UIView
(for your callout).
然后创建<$ c $的子类c> MKAnnotationView 并覆盖 setSelected
,如下所示:
Then create a subclass of MKAnnotationView
and override setSelected
as follows:
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
if(selected)
{
//Add your custom view to self...
}
else
{
//Remove your custom view...
}
}
繁荣,完成工作。
这篇关于如何自定义MKAnnotationView的标注气泡?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!