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];

これに関する問題は、ピンの背後にある間、マップが再配置されるとすぐに画像が画面から浮き上がることです。

助言がありますか?ありがとう

4

3 に答える 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;
}

必要なピンは 1 つだけだったので、 に設定reuseIdentifierしましたnil

于 2009-10-04T02:44:57.827 に答える
4

「前面」イメージの背後に別のイメージを追加するために、 and をサブクラス化してandメソッドをMKAnnotatonViewオーバーライドしました。initWithAnnotation:resueIdentifierdrawRect

それが、Apple のMKAnnotationViewクラス リファレンスが推奨していることのようです。

トリッキーなのはそれだけです。をサブクラス化する場合MKAnnotationView、image プロパティはまだ存在しています。作画と連動するようにちゃんと工夫されています。おそらく、初期化が完了した後に画像を描画するために drawRect をオーバーライドしました。

また、image プロパティを設定しない場合、カスタムのフレームは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に、ピンとその影を表示するための独自の描画方法があります。

おそらく、self.image プロパティから Pin 画像を取得できると思います。影に関しては、私は確信が持てません... OPEN GL描画メソッドを使用して影を追加するか、単に影の画像を組み合わせている可能性があります。

最後に、画像にはアニメーションが付いています。アニメーションが実行される場所だと思います。ただし、この段階ではまだテストしていません。

于 2010-02-09T01:20:08.377 に答える