我继承了MKAnnotatonView 并覆盖了initWithAnnotation:resueIdentifier 和drawRect 方法以在“正面”图像后面添加另一个图像。
这似乎是 Apple 的 MKAnnotationView 类参考建议我们做的事情。
只是它很棘手。如果您将MKAnnotationView 子类化,则图像属性仍然存在。他们已经做了一些适当的事情,以便与绘图联系起来。也许,他们已经覆盖了 drawRect 以在初始化完成后绘制图像。
另外,如果你不设置图片属性,你自定义的annotationView的框架会设置为0,drawRect方法不会被调用。
最后,Apple 表示图像应该被完全填充,即使用透明颜色作为绘图的背景。
所以:在你的子类中:
- (id)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString *)identifier
{
self = [super initWithAnnotation:annotation reuseIdentifier:identifier];
if (self)
{
UIButton *rightButton = [UIButton buttonWithType: UIButtonTypeDetailDisclosure];
[rightButton addTarget:self
action:@selector(myShowAnnotationAddress:)
forControlEvents:UIControlEventTouchUpInside];
self.rightCalloutAccessoryView = rightButton;
self.canShowCallout = YES;
self.multipleTouchEnabled = NO;
self.frame = CGRectMake(0, 0, 65, 100);
self.backgroundColor = [UIColor clearColor];
return self;
}
return nil;
}
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
CGPoint drawPoint = CGPointMake(0.0f, 0.0f);
UIImage *shadowImage = [UIImage imageNamed:@"shadow_image.png"];
[shadowImage drawAtPoint:drawPoint];
UIImage *frontImage = [UIImage imageNamed:@"front_image.png"];
[frontImage drawAtPoint:drawPoint];
CGContextRestoreGState(context);
}
在我做出回答后,我意识到你其实是想在MKPinAnnotationView 后面画图。我的回答不是这样,尽管它显示了可能应该在哪里绘制。显然MKPinAnnottions 有自己的绘制方法来呈现 Pin 及其阴影。
我认为您可能可以从 self.image 属性中检索 Pin 图。至于阴影,我不确定……可能是用OPEN GL的绘制方法来添加阴影,或者只是组合一个阴影图像。
最后,图像带有动画。我猜那是动画运行的地方。不过,在这个阶段,我还没有测试过。