【问题标题】:Add image behind MKPinAnnotationView在 MKPinAnnotationView 后面添加图片
【发布时间】:2010-12-03 06:11:37
【问题描述】:

我正在尝试在 MKPinAnnotationView 后面添加图像。似乎在这里这样做应该很容易:

- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views {
 for (MKAnnotationView *aView in views)
  [[aView superview] addSubview:imageView];
}

但我这样做的问题是,pin 的子子视图将呈现在它的顶部而不是它的后面。

我也试过了:

 for (MKAnnotationView *aView in views)
  [[aView superview] insertSubview:imageView atIndex:1];

问题在于,虽然它位于图钉后面,但一旦重新定位地图,图像就会浮出屏幕。

有什么建议吗?谢谢

【问题讨论】:

    标签: iphone mkmapview mkpinannotationview


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      - (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 这样做?
      • 如果没有 MKPinAnnotationView,MKAnnotationView 不会响应触摸。如何解决问题?
      【解决方案3】:

      我继承了MKAnnotatonView 并覆盖了initWithAnnotation:resueIdentifierdrawRect 方法以在“正面”图像后面添加另一个图像。

      这似乎是 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的绘制方法来添加阴影,或者只是组合一个阴影图像。

      最后,图像带有动画。我猜那是动画运行的地方。不过,在这个阶段,我还没有测试过。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-10-18
        • 2017-08-15
        • 1970-01-01
        • 2017-09-10
        • 2011-03-15
        • 1970-01-01
        相关资源
        最近更新 更多