在 MKPinAnnotationView 后面添加图像

发布于 2024-08-07 01:55:22 字数 492 浏览 7 评论 0原文

我正在尝试在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

他是夢罘是命 2024-08-14 01:55:22

感谢您的输入,这基本上是我在没有子类化的情况下最终所做的事情:

- (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

Thanks for the input, here's basically what I ended up doing without subclassing:

- (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;
}

I only needed one pin, so I set reuseIdentifier to nil.

伏妖词 2024-08-14 01:55:22

我对 MKAnnotatonView 进行了子类化,并重写了 initWithAnnotation:resueIdentifier 和 drawRect 方法,以在“正面”图像后面添加另一个图像。

看起来这就是 Apple 的 MKAnnotationView 类参考建议我们做的事情。

只是这很棘手。如果您要子类化 MKAnnotationView,则图像属性仍然存在。他们做了一些适当的事情,以便将其与绘画联系起来。也许,他们在初始化完成后重写了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后面绘制。我的答案不是这样,尽管它显示了可能应该在哪里绘制。显然 MKPinAnnotions 有自己的绘制方法来呈现 Pin 及其阴影。

我认为您可能可以从 self.image 属性中检索 Pin 图像。至于阴影,我不确定......可能是使用OPEN GL绘图方法添加阴影,或者简单地组合阴影图像。

最后,图像带有动画。我猜这就是动画运行的地方。但在现阶段,我还没有进行测试。

I have subclassed the MKAnnotatonView and overrode the initWithAnnotation:resueIdentifier and drawRect 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 the drawRect 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:

- (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);
}

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. Obviously MKPinAnnottions 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.

雨落星ぅ辰 2024-08-14 01:55:22

创建一个新的复合注释视图,首先添加您的图像,然后添加实际的 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  

Create a new composite annotationview that first adds your image and then the actual 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  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文