在MKPinAnnotationView后添加图像

3

我正在尝试将一张图片添加到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];

这样做的问题在于,虽然它确实在图钉后面,但一旦地图被重新定位,图片就会漂离屏幕。
有什么建议吗?谢谢。
3个回答

7

感谢您的输入,以下是我最终采取的做法,没有使用子类:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id )annotation {

    MKAnnotationView *annView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil];

    UIImage *image = [UIImage imageNamed:@"image.png"];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    [annView addSubview:imageView];
    [imageView release];

    MKPinAnnotationView *pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil];     
    [annView addSubview:pinView];
    [pinView release];

    return annView;
}

我只需要一个引脚,所以我将reuseIdentifier设置为nil


你如何通过这样做来隐藏 PIN? - ingh.am
如果没有MKPinAnnotationView,MKAnnotationView将不会响应触摸。如何解决这个问题? - Satyam

4

我已经对MKAnnotatonView进行了子类化,并覆盖了initWithAnnotation:resueIdentifierdrawRect方法,以在“前面”图像后添加另一张图像。

看起来这就是苹果的MKAnnotationView类参考所建议的做法。

但是这很棘手。如果你正在对MKAnnotationView进行子类化,那么仍然存在image属性。他们已经对该属性进行了一些处理,使其与绘制相关联。也许,他们已经覆盖了drawRect方法,在初始化完成后绘制图像。

此外,如果您没有设置image属性,则自定义annotationView的框架大小将被设置为0,并且drawRect方法不会被调用。

最后,苹果表示图像应完全填充,即使用透明颜色作为绘画背景。

因此,在您的子类中:

- (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有其自己的绘制方法来呈现一个图钉及其阴影。
我认为你可以从self.image属性中检索出图钉图像。至于阴影,我不确定......它可能使用OPEN GL绘图方法添加阴影,或者简单地组合一个阴影图像。
最后,图像带有动画效果。我猜测那就是动画运行的地方。不过,在这个阶段,我还没有测试过。

3
创建一个新的复合注释视图,首先添加您的图像,然后再添加实际的MKAnnotationView:
@implementation MyCustomAnnotationView

- (void) viewDidLoad 
{   
    // First add the image to our subviews
    UIImageView *view = [[UIImageView alloc] initWithImage: myImageProperty];
    [self.view addSubview: view];
    [view release];

    // Then add the MKAnnotationView on top of it
    MKAnnotationView *annotation = [[MKAnnotationView alloc] init]; 
    [self.view addSubview: annotation]; 
    [annotation release];
}                        

@end  

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接