自定义 MKAnnotationView 标注
我想创建一个自定义 MKAnnotationView 标注,如此图所示。我已经测试了几种解决方案,但它们只允许自定义左/右图像和标题/副标题。有人可以给我一些源代码或教程链接吗?
目前我一无所知。请帮忙。
I want to create a custom MKAnnotationView callout as shown in this image. I have tested several solutions but they only allow customization of the left/right images and title/subtitle. Can anybody please give me some source code or tutorial link for it?
Currently I am clueless. Please help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我了解您想要带有自定义标注的图钉。
我们无法创建自定义标注,但我们可以使用完全自定义的视图创建注释。因此,诀窍是在选择第一个注释时添加第二个注释,并使第二个注释视图看起来像标注气泡。
这是用户 djibouti33 和 雅各布-詹宁斯 在答案中:MKAnnotationView - 锁定自定义注释视图以固定位置更新,而这又基于 来自异步解决方案的博客文章。出于解释目的,以下是来自分叉项目的一些 UML:
这是一个很大的技巧,但也是我见过的实现自定义注释的最干净的方法。
从一个 NSObject“Content”类开始,它有一个坐标、要使用的标注视图的类(在 UML 中是 AnnotationView,但您可以创建更多并在此处设置它们)以及带有标题、照片的随机值字典url等。使用此类来初始化MKAnnotation“Annotation”对象。
Annotation 实现 AnnotationProtocol 来宣布它想要处理自己的 MKAnnotationView 的创建。也就是说,您的 MKMapViewDelegate 应该具有如下代码:
返回的视图将是 AnnotationView 类型,它实现 AnnotationViewProtocol 来宣布它想要处理选择/取消选择。因此,在地图视图控制器中,方法mapView:didSelectAnnotationView:和mapView:didDeselectAnnotationView:应该以与我们之前看到的类似的方式进行委托。
选择注释时,会添加第二个注释 (CalloutAnnotation),它遵循相同的行为,但这次返回的视图 (CalloutView) 是从 XIB 初始化的,并包含 Core Graphics 代码(在 BaseCalloutView 中)以进行动画处理和复制大喊。
CalloutView 类的初始化程序:
为了能够从标注视图中推送另一个视图控制器,我使用了通知。
我在顶部链接的 SO 答案包含两个实现此代码的完整项目(类名可能不同)。我有另一个项目使用上面的 UML https://github.com/j4n0/callout。
I understand you want a pin with a custom callout.
We can't create a custom callout, but we can create an annotation with a completely customized view. So the trick is to add a second annotation when the first is selected, and make the 2nd annotation view look like a callout bubble.
This is the solution posted by users djibouti33 and jacob-jennings in the answer: MKAnnotationView - Lock custom annotation view to pin on location updates, which in turn is based in a blog post from Asynchrony Solutions. For explanation purposes, here is some UML from a forked project:
This is a big hack, but also the cleanest way I've seen to implement custom annotations.
Start with a NSObject "Content" class which has a coordinate, the class of the callout view to use (in the UML is AnnotationView, but you can create more and set them here), and a dictionary of random values with the title, photo url, etc. Use this class to initialize a MKAnnotation "Annotation" object.
The Annotation implements AnnotationProtocol to announce it wants to handle the creation of its own MKAnnotationView. That is, your MKMapViewDelegate should have code like this:
The view returned will be of type AnnotationView, which implements AnnotationViewProtocol to announce that it wants to handle selection/deselection. Therefore, in your map view controller, the methods mapView:didSelectAnnotationView: and mapView:didDeselectAnnotationView: should delegate in a similar way to what we saw before.
When the annotation is selected, a second annotation (CalloutAnnotation) is added, which follows the same behaviour, but this time the view returned (CalloutView) is initialized from a XIB, and contains Core Graphics code (in BaseCalloutView) to animate and replicate a callout.
The initializer of the CalloutView class:
To be able to push another view controller from the callout view I used notifications.
The SO answer I linked at the top contains two complete projects implementing this code (class names may differ). I have another project using the UML above at https://github.com/j4n0/callout.
我在 MKAnnotationView 中添加了自定义 UIButton。单击该按钮后,我显示了带有 rootViewController 的 popOver,其视图与上面所示的类似。
I added custom UIButton in MKAnnotationView. And on click of that button I have shown popOver with rootViewController with the view similar as you have shown above.
我知道这个问题是 2011 年提出的,但对于仍然在搜索中找到它的人来说:
在 iOS 9 中,您可以使用
MKAnnotationView.detailCalloutAccessoryView
来完全取代标准标注。I know this question is from 2011 but for people who still find it in a search:
In iOS 9 you have
MKAnnotationView.detailCalloutAccessoryView
which entirely replaces the standard callout.