2009-10-02 69 views
3

我試圖在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]; 

這裏的問題是,雖然它在銷的背後,一旦地圖被重新定位,圖像漂浮在屏幕上。

有什麼建議嗎?由於

回答

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

+0

如何隱藏這個引腳? – 2010-02-12 15:16:32

+0

如果沒有MKPinAnnotationView,MKAnnotationView將不會響應觸摸。如何解決問題? – Satyam 2014-02-15 18:19:24

3

創建一個新的複合annotationview首先將您的圖像,然後實際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 
+0

謝謝,我會試試看。 – Adolfo 2009-10-02 11:16:14

4

我已將子類別MKAnnotatonView並覆蓋了initWithAnnotation:resueIdentifierdrawRect方法在「前」圖像後添加另一圖像。

看來這就是蘋果的MKAnnotationView類參考所暗示的我們所要做的。

只是它很棘手。如果您繼承MKAnnotationView,仍然存在圖像屬性。他們已經做了一些正確的事情,以便它與繪圖相關聯。也許,他們已經重寫了drawRect在初始化完成後繪製圖像。

此外,如果您未設置圖像屬性,則自定義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有它自己的繪圖方法來呈現一個Pin和它的影子。

我想這可能是你可以從self.image屬性中獲取Pin圖像。至於陰影,我不確定...它可能使用OPEN GL繪圖方法來添加陰影,或者僅僅是組合陰影圖像。

最後,圖像自帶動畫。我猜測那是動畫運行的地方。但是在這個階段,我還沒有測試過。