在 MKPinAnnotationView 后面添加图像
我正在尝试在 MKPinAnnotationView 后面添加图像。似乎在这里执行此操作应该相当容易:
- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views {
for (MKAnnotationView *aView in views)
[[aView superview] addSubview:imageView];
}
但我这样做时遇到的问题是引脚的子子视图将呈现在它的顶部而不是后面。
我也尝试过:
for (MKAnnotationView *aView in views)
[[aView superview] insertSubview:imageView atIndex:1];
这样做的问题是,虽然它位于图钉后面,但一旦重新定位地图,图像就会浮出屏幕。
有什么建议吗?谢谢
I'm trying to add an image behind a MKPinAnnotationView. Seems like it should be rather easy to just do this in here:
- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views {
for (MKAnnotationView *aView in views)
[[aView superview] addSubview:imageView];
}
But the problem I am having in doing that is that the child subview to the pin will render on top of it not BEHIND it.
I have also tried:
for (MKAnnotationView *aView in views)
[[aView superview] insertSubview:imageView atIndex:1];
The problem with this is that, while it IS behind the pin, as soon as the map is repositioned, the image floats off the screen.
Any suggestions? Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
感谢您的输入,这基本上是我在没有子类化的情况下最终所做的事情:
我只需要一个引脚,所以我将
reuseIdentifier
设置为nil
。Thanks for the input, here's basically what I ended up doing without subclassing:
I only needed one pin, so I set
reuseIdentifier
tonil
.我对 MKAnnotatonView 进行了子类化,并重写了 initWithAnnotation:resueIdentifier 和 drawRect 方法,以在“正面”图像后面添加另一个图像。
看起来这就是 Apple 的 MKAnnotationView 类参考建议我们做的事情。
只是这很棘手。如果您要子类化
MKAnnotationView
,则图像属性仍然存在。他们做了一些适当的事情,以便将其与绘画联系起来。也许,他们在初始化完成后重写了drawRect来绘制图像。另外,如果您不设置 image 属性,则自定义
annotationView
的框架将设置为大小 0,并且不会调用drawRect
方法。最后,苹果表示图像应该完全填充,即使用透明颜色作为绘图的背景。
所以:在你的子类中:
在我做出答案之后,我意识到你实际上想在
MKPinAnnotationView
后面绘制。我的答案不是这样,尽管它显示了可能应该在哪里绘制。显然 MKPinAnnotions 有自己的绘制方法来呈现 Pin 及其阴影。我认为您可能可以从 self.image 属性中检索 Pin 图像。至于阴影,我不确定......可能是使用OPEN GL绘图方法添加阴影,或者简单地组合阴影图像。
最后,图像带有动画。我猜这就是动画运行的地方。但在现阶段,我还没有进行测试。
I have subclassed the
MKAnnotatonView
and overrode theinitWithAnnotation:resueIdentifier
anddrawRect
methods to add another image behind the "front" image.It seems that that is what Apple's
MKAnnotationView
class reference is suggesting us to do.Only that it is tricky. If you are subclassing the
MKAnnotationView
, there is still the image property existing. They have done something about the properly so that it is linked with drawing. Perhaps, they have overridden the drawRect to draw the image AFTER the initialization is done.Also, if you don't set the image property, the frame of your custom
annotationView
is set to size 0, and thedrawRect
method does not get called.Finally, Apple says that the images should be totally filled, i.e. uses a transparent colour for the background of the drawings.
So: In your subclass:
After I made the answer, I realize that you actually wanted to draw behind
MKPinAnnotationView
. My answer is not that, although it shows where the drawing perhaps should be done. ObviouslyMKPinAnnottions
has its own drawing method to present a Pin and its shadow.I think that probably you can retrieve the Pin image from the self.image property. As to the shadow, I am not certain... It may be using OPEN GL drawing method to add the shadow, or simply combining a shadow image.
Finally, the image comes with animation. I am guessing that that is where the animation is run. At this stage, though, I have not tested.
创建一个新的复合注释视图,首先添加您的图像,然后添加实际的 MKAnnotationView:
Create a new composite annotationview that first adds your image and then the actual MKAnnotationView: